-2

I wonder if possible to execute (run,call,activate..) the function, after when php detects that :
1) $something varialbe is being defined
or
2) something other function is executed. See example:

function sayy()
   {
   echo "Hiiiiiii";
   }
RUN FUNCTION when $VARIALBE will be set;
......
......
......
......
......
......
include('blablabla.php');
//i dont have access to blablabla.php,and there is code:
$VARIALBE = 'Jake';
!!!!!!!!!THEN I WANT EXECUTE MY FUNCTION HERE!!!!!!!!!!!
T.Todua
  • 53,146
  • 19
  • 236
  • 237
  • 1
    How is this different than just calling the function: `sayy()`? – drew_w May 02 '14 at 16:26
  • 1
    Typically that would be handled through a class, which has function `setVariable($value)`. Then you can do whatever logic you want on change of the variable. – mellamokb May 02 '14 at 16:27
  • 1
    There aren't events such as "variable set" for you to attach onto. Your only hope here would be to do like mellamokb suggested and use a class. Either with a setter method as described or using the php magic method `__set` that gets called when a property is set. – Jonathan Kuhn May 02 '14 at 16:31

2 Answers2

2

Sure:

function sayy($value) {
  echo "Hi.";
  return $value;
}

$VARIABLE = sayy('Jake');

Or in a sane, object-oriented manner, see Why use getters and setters?

Basically, you set your variable to private and make a public function that sets the variable and also does whatever you want your function to do. PHP has a set of "magic functions" that will allow you to do this without specifically writing a getter/setter for every variable, eg: __set()

Community
  • 1
  • 1
Sammitch
  • 30,782
  • 7
  • 50
  • 77
0

The best way to handle your need is to learn OOP. This a good place to start. KillerPHP

nazim
  • 1,439
  • 2
  • 16
  • 26