Pre-configuration of functions

Asked

Viewed 30 times

0

Hello, I have a facebook BOT and I created a function called enviaMSG(); and within it I have 2 parameters, the user’s facebook id and the message to send but in any code I use this function a lot and I sometimes forget to pass the facebook id, you can already set the facebook id at the beginning?

  • You can set the parameter value as default in the function setting.

1 answer

0


You can do this in a few different ways.

DEFINE A CONSTANT or variable and then call it in your code. Ex.:

<?php
define("ID_FACEBOOK","SEU_ID_AQUI");
...
enviaMSG(ID_FACEBOOK,"Você está procurando por um hotel?");
?>

Create a CLASS for its related functions and then be able to use the data saved within it. Ex.:

<?php
class uteisFacebook{
    public $id_facebook="";

    public function enviaMSG($msg){
        ... sua função aqui, mas quando for chamar o ID use: $this->id_facebook
    }
}
?>

Then you configure the class in your code, if you have a configuration file that is called on every page you can do this within it, so you don’t always need to reconfigure.

<?php
// Inclua o arquivo da classe:
include("uteisFacebook.php");
// Chame a classe e defina suas variáveis
$facebook=new uteisFacebook();
$facebook->id_facebook="defina_o_id_aqui";
?>

And Finally to call the function:

<?php
$facebook->enviaMSG("Você tem um tempinho para falar do nosso senhor e criador, Markinho da galera?");
?>

Well, these are just examples to help you get where you want, but in case you use class I recommend to do a better search on how to build a class.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.