Friday 15 January 2010

php - passing function by reference from within class -


I pass a function (from within the same class) by reference to any other function of that function I was trying to, though it does not seem to work, I can do this outside of a classroom, but not within the classroom: Is there a way to do it properly?

  class test {function test () {$ this-> Check (1 = = 2, and $ this-> T (), & amp; $ this-> f ()); } Function check ($ statement, $ true, and $ false) {if (eval ($ statement)) {$ true; } $ False; } Festival T () {print "true"; Back true; } Function F () {print "wrong"; return false; }}    

Passed by reference on call-time (and indeed 5.4 ):

In PHP 5.3.0, you will receive a warning that "call-time pass-by-reference" is rejected when you & Amp; In Fu (& amp; $ a); And as PHP 5.4.0, the call-time pass-by-reference was removed, so using it will cause a serious error.

There is no real point in passing - a reference for a boolean will carry a PHP context in memory, in theory, similar to the C-pinter (4 bytes), a boolean would probably be a small int, in which only 2 Bytes are required. If there is anything, then I would say that you are producing more land.

If you are passing object or array in the context of any other function / method, then you have to define fun cats accordingly: function doStuff_byRef (array & Amp; $ arg) {$ arg => = 'Add new stuff'; }

Passing functions from context, as you would in JS, is not possible in PHP However, you do not not a function to return a value Can define but return the reference value of its return value:

  public function and getPropertyRef () {$ $ this - & gt; Property; }   

In your case, however, I will do just that:

  $ this-> Check (1 == 2, 't', 'f'); // Pass method name Private function check ($ STMT, $ func1, $ func2) {// do not use EVAL, if BTW ($ STMT) {return $ this- & gt; {$ Func1} (); } $ $ Return-> {$ func2} (); }   

Or, even more flexible:

  $ this-> Check (1 == 2, array ($ this, 't'), array ($ this, 'f')); // pass method name private function check ($ stmt, array $ func1, array $ func2) {// do not use eval, if BTW ($ stmt) {return call_user_func_array ($ func1); } Return call_user_func_array ($ func2); }   

I think, if you have PHP & gt; = 5.3, you can use the closure instance:

  Private function check ($ stmt, close & amp;; Func1, close & amp; $ func2) {return $ stmt ? $ Func1 (): $ func2 (); }   

Then, you can:

  $ functionReference = function () {echo 'true'; Back true; }; $ FunctionRef2 = function () {echo 'false'; return false; }; $ This- & gt; Check (true, $ function, reference, $ functionRef2);   

But adding more weight: PHP closing is one of those things which were added in the language, that should not be there this Creating an example of the closing class on the compilation of time, and that object is being passed from context to honestly, the best way to go to your case is my first suggestion, however , IMHO ( $ this-> {$ func1} (); )

No comments:

Post a Comment