Re: [PHP] Variable functions within an object
Curt Zirzow wrote: $this-{$this-fname}(); or (what it actually is doing.. ) $func = $this-fname; $this-$func(); Curt The point here is that the named function is outside the object. That is, $this-foo() doesn't exist, so $this-{$this-fname}(), does not work either. But if you look at your suggested construct, I wonder why $this-{$this-fname}() is sintactically correct while {$this-fname}() is not (since we just striped out the prefix '$this-' which means that the function is inside the object). Look for instance at a similar case when dealing with variable variables in arrays, this is what the online documentation says in this case: In order to use variable variables with arrays, you have to resolve an ambiguity problem. That is, if you write $$a[1] then the parser needs to know if you meant to use $a[1] as a variable, or if you wanted $$a as the variable and then the [1] index from that variable. The syntax for resolving this ambiguity is: ${$a[1]} for the first case and ${$a}[1] for the second. So, I think the case is similar here Clearly, $this-fname() means call the object's method 'fname', but {$this-fname}(), would mean call the function whose name is '$this-fname'. I think, for the sake of orthogonallity, this feature should be added to the language. Thank you. Sergio. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Variable functions within an object
* Thus wrote Julio Sergio Santana: Curt Zirzow wrote: $this-{$this-fname}(); or (what it actually is doing.. ) $func = $this-fname; $this-$func(); Curt The point here is that the named function is outside the object. That is, $this-foo() doesn't exist, so $this-{$this-fname}(), does not work either. But if you look at your suggested construct, I wonder why $this-{$this-fname}() is sintactically correct while {$this-fname}() is not (since we just striped out the prefix '$this-' which means that the function is inside the object). Sorry, i did misread what actual function you were trying to access, the problem is that {} isn't really the thing that expands the variable its the special cases: -{} ${} {} by itself simply defines a code block. ... Clearly, $this-fname() means call the object's method 'fname', but {$this-fname}(), would mean call the function whose name is '$this-fname'. I think, for the sake of orthogonallity, this feature should be added to the language. One way to solve this without adding a feature like that would be: ${$this-fname} = $this-fname; ${$this-fname}(); or for the oneline purists :) ${ ${$this-fname} = $this-fname }(); wow.. ${} is more powerful than i had originally though. Curt -- First, let me assure you that this is not one of those shady pyramid schemes you've been hearing about. No, sir. Our model is the trapezoid! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Variable functions within an object
Curt Zirzow wrote: or for the oneline purists :) ${ ${$this-fname} = $this-fname }(); wow.. ${} is more powerful than i had originally though. Thank you Curt, With your suggestion, I finally re-wrote the example, and here it is: ?php function foo() { echo In foo()br /\n; } class a { var $fname; function a() { $this-fname = 'foo'; // the name of the function } function execute() { // method to execute the named function ${${$this-fname}=$this-fname}(); } } $w = new a; $w-execute(); ? And this outputs: X-Powered-By: PHP/4.1.2 Content-type: text/html In foo()br / Thank you again, Sergio. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Variable functions within an object
I need to record the names of functions, and then use them later. Recently I found the following example within the on-line documentation: ?php function foo() { echo In foo()br /\n; } $func = 'foo'; $func();// This calls foo() ? then I supposed that it was easy to extend this concept to objects and wrote the following case: ?php function foo() { echo In foo()br /\n; } class a { var $fname; function a() { $this-fname = 'foo'; // the name of the function } function execute() { // method to execute the named function $this-fname(); // I also tried here // {$this-fname}(); // ${this-fname}(); // $this-fname(); // but none of these worked } } $w = new a; $w-execute(); ? And this was the error I got: X-Powered-By: PHP/4.1.2 Content-type: text/html br bFatal error/b: Call to undefined function: fname() in b-/b on line b14/bbr I know that this can be solved easily with an intermediate variable: $temp = $this-fname; $temp(); but I wonder if there is a more direct method. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Variable functions within an object
* Thus wrote Julio Sergio Santana: class a { var $fname; function a() { $this-fname = 'foo'; // the name of the function } function execute() { // method to execute the named function $this-fname(); // I also tried here // {$this-fname}(); // ${this-fname}(); // $this-fname(); $this-{$this-fname}(); or (what it actually is doing.. ) $func = $this-fname; $this-$func(); Curt -- First, let me assure you that this is not one of those shady pyramid schemes you've been hearing about. No, sir. Our model is the trapezoid! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Variable functions within an object
http://ca3.php.net/manual/en/function.call-user-func.php ? Jason On Thu, 29 Jul 2004 17:11:50 -0500, Julio Sergio Santana [EMAIL PROTECTED] wrote: I need to record the names of functions, and then use them later. Recently I found the following example within the on-line documentation: ?php function foo() { echo In foo()br /\n; } $func = 'foo'; $func();// This calls foo() ? then I supposed that it was easy to extend this concept to objects and wrote the following case: ?php function foo() { echo In foo()br /\n; } class a { var $fname; function a() { $this-fname = 'foo'; // the name of the function } function execute() { // method to execute the named function $this-fname(); // I also tried here // {$this-fname}(); // ${this-fname}(); // $this-fname(); // but none of these worked } } $w = new a; $w-execute(); ? And this was the error I got: X-Powered-By: PHP/4.1.2 Content-type: text/html br bFatal error/b: Call to undefined function: fname() in b-/b on line b14/bbr I know that this can be solved easily with an intermediate variable: $temp = $this-fname; $temp(); but I wonder if there is a more direct method. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Variable Functions...
On Friday, July 11, 2003, 3:11:51 AM, Michael wrote: MS Smarty has a class method where it calls: $this-$some_var(somevalue); Are you sure about that syntax? I'm not too familiar with Smarty, only used it once, but I think its $this-some_var(value); MS and this throws errors on Windows versions of php that i've tried. why MS is that? Does it error out on l/unix? -- Regards, Burhan Khalid phplist[at]meidomus[dot]com http://www.meidomus.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Variable Functions...
Smarty has a class method where it calls: $this-$some_var(somevalue); and this throws errors on Windows versions of php that i've tried. why is that? -Michael -- Pratt Museum IT Intern All programmers are playwrights and all computers are lousy actors. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] variable functions: empty/isset/unset invalid?
This does not work as expected (as I expect it at least) and gives the following error. This seems to result with use of empty(), isset(), and unset(), perhaps others : Call to undefined function: empty() When using : $foo = 'empty'; if ($foo($var)) print 'worked.'; Of course the following works as expected. if (empty($var)) print 'worked.'; And with other functions : $foo = 'somefunction'; if ($foo($var)) print 'worked.'; Why won't variable functions work with empty/isset/unset this way? And why the 'undefined function' fatal error? regards, philip -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP] variable functions: empty/isset/unset invalid?
$foo is a string not a PHP function which makes 'empty' a string and not a function/command. Am I missing something? oktay -Original Message- From: Philip Olson [mailto:[EMAIL PROTECTED]] Sent: Monday, May 07, 2001 1:04 PM To: [EMAIL PROTECTED] Subject: [PHP] variable functions: empty/isset/unset invalid? This does not work as expected (as I expect it at least) and gives the following error. This seems to result with use of empty(), isset(), and unset(), perhaps others : Call to undefined function: empty() When using : $foo = 'empty'; if ($foo($var)) print 'worked.'; Of course the following works as expected. if (empty($var)) print 'worked.'; And with other functions : $foo = 'somefunction'; if ($foo($var)) print 'worked.'; Why won't variable functions work with empty/isset/unset this way? And why the 'undefined function' fatal error? regards, philip -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP] variable functions: empty/isset/unset invalid?
Change the parens around $var to curly braces: if ($foo{$var}) print 'worked.'; Kirk -Original Message- From: Philip Olson [mailto:[EMAIL PROTECTED]] Subject: [PHP] variable functions: empty/isset/unset invalid? This does not work as expected (as I expect it at least) and gives the following error. This seems to result with use of empty(), isset(), and unset(), perhaps others : Call to undefined function: empty() When using : $foo = 'empty'; if ($foo($var)) print 'worked.'; -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP] variable functions: empty/isset/unset invalid?
I wish it were that easy. Also, I'm looking for words on WHY this behavior exists. http://www.php.net/manual/en/functions.variable-functions.php ?php // works if (empty($var)) print '$var is emptybr'; // works $foo = 'is_string'; $var = 'abcdef'; if ($foo($var)) print '$var is a stringbr'; // works $foo = 'strlen'; $var = 'abcdef'; if ($foo($var) 5) print '$var is over 5 charsbr'; // doesn't work : Fatal Error : Call to undefined function: empty() // same with isset() and unset() $foo = 'empty'; if ($foo($var)) print '$var is empty'; ? In otherwords, only these few functions aren't working as variable functions but result in a Fatal Error instead. Why? Regards, Philip On Mon, 7 May 2001, Johnson, Kirk wrote: Change the parens around $var to curly braces: if ($foo{$var}) print 'worked.'; Kirk -Original Message- From: Philip Olson [mailto:[EMAIL PROTECTED]] Subject: [PHP] variable functions: empty/isset/unset invalid? This does not work as expected (as I expect it at least) and gives the following error. This seems to result with use of empty(), isset(), and unset(), perhaps others : Call to undefined function: empty() When using : $foo = 'empty'; if ($foo($var)) print 'worked.'; -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP] variable functions: empty/isset/unset invalid?
Thanks for the additional code, now I see what you are after. Sorry, I don't know the answer, other than using curly braces will fix the problem for empty(). Also, a User Contributed Note at http://www.php.net/manual/en/functions.php#functions.user-defined has this to say: quote there are tons of good uses for this sort of functionality. But it should be noted that this will not work with include() include_once() require() require_once() it's safe to assume that this is for safty. /quote Kirk -Original Message- From: Philip Olson [mailto:[EMAIL PROTECTED]] Sent: Monday, May 07, 2001 11:45 AM To: [EMAIL PROTECTED] Subject: RE: [PHP] variable functions: empty/isset/unset invalid? I wish it were that easy. Also, I'm looking for words on WHY this behavior exists. http://www.php.net/manual/en/functions.variable-functions.php ?php // works if (empty($var)) print '$var is emptybr'; // works $foo = 'is_string'; $var = 'abcdef'; if ($foo($var)) print '$var is a stringbr'; // works $foo = 'strlen'; $var = 'abcdef'; if ($foo($var) 5) print '$var is over 5 charsbr'; // doesn't work : Fatal Error : Call to undefined function: empty() // same with isset() and unset() $foo = 'empty'; if ($foo($var)) print '$var is empty'; ? In otherwords, only these few functions aren't working as variable functions but result in a Fatal Error instead. Why? Regards, Philip On Mon, 7 May 2001, Johnson, Kirk wrote: Change the parens around $var to curly braces: if ($foo{$var}) print 'worked.'; Kirk -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP] variable functions: empty/isset/unset invalid?
Solved. empty, isset and unset are not functions, they are language contructs, which results in the error. This makes sense, a workaround is creating functions like isEmpty (or something similar) and using them. I'll be submitting a future request soon ;) Thanks everyone, especially OpenSrc in #php regards, Philip snip ?php // works if (empty($var)) print '$var is emptybr'; // works $foo = 'is_string'; $var = 'abcdef'; if ($foo($var)) print '$var is a stringbr'; // works $foo = 'strlen'; $var = 'abcdef'; if ($foo($var) 5) print '$var is over 5 charsbr'; // doesn't work : Fatal Error : Call to undefined function: empty() // same with isset() and unset() $foo = 'empty'; if ($foo($var)) print '$var is empty'; ? In otherwords, only these few functions aren't working as variable functions but result in a Fatal Error instead. Why? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP] variable functions: empty/isset/unset invalid?
Though I didn't know about 'variable functions' when answered, this is exactly what I said.. Interesting :) $foo is a string not a PHP function which makes 'empty' a string and not a function/command. Am I missing something? oktay -Original Message- From: Philip Olson [mailto:[EMAIL PROTECTED]] Sent: Monday, May 07, 2001 2:43 PM To: [EMAIL PROTECTED] Subject: RE: [PHP] variable functions: empty/isset/unset invalid? Solved. empty, isset and unset are not functions, they are language contructs, which results in the error. This makes sense, a workaround is creating functions like isEmpty (or something similar) and using them. I'll be submitting a future request soon ;) Thanks everyone, especially OpenSrc in #php regards, Philip snip ?php // works if (empty($var)) print '$var is emptybr'; // works $foo = 'is_string'; $var = 'abcdef'; if ($foo($var)) print '$var is a stringbr'; // works $foo = 'strlen'; $var = 'abcdef'; if ($foo($var) 5) print '$var is over 5 charsbr'; // doesn't work : Fatal Error : Call to undefined function: empty() // same with isset() and unset() $foo = 'empty'; if ($foo($var)) print '$var is empty'; ? In otherwords, only these few functions aren't working as variable functions but result in a Fatal Error instead. Why? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] variable functions: empty/isset/unset invalid?
Thanks for the additional code, now I see what you are after. Sorry, I don't know the answer, other than using curly braces will fix the problem for empty(). Also, a User Contributed Note at http://www.php.net/manual/en/functions.php#functions.user-defined has this to say: quote there are tons of good uses for this sort of functionality. But it should be noted that this will not work with include() include_once() require() require_once() it's safe to assume that this is for safty. /quote These funtions are directives rather than normal php functions as the manual says. I'm just guessing, maybe empty(), isset() and other variable-testing functions are also directives with a function-syntax wrapper and this is the reason why PHP doesn't find them as functions (not in the target-list :). Kirk -Original Message- From: Philip Olson [mailto:[EMAIL PROTECTED]] Sent: Monday, May 07, 2001 11:45 AM To: [EMAIL PROTECTED] Subject: RE: [PHP] variable functions: empty/isset/unset invalid? I wish it were that easy. Also, I'm looking for words on WHY this behavior exists. http://www.php.net/manual/en/functions.variable-functions.php ?php // works if (empty($var)) print '$var is emptybr'; // works $foo = 'is_string'; $var = 'abcdef'; if ($foo($var)) print '$var is a stringbr'; // works $foo = 'strlen'; $var = 'abcdef'; if ($foo($var) 5) print '$var is over 5 charsbr'; // doesn't work : Fatal Error : Call to undefined function: empty() // same with isset() and unset() $foo = 'empty'; if ($foo($var)) print '$var is empty'; ? In otherwords, only these few functions aren't working as variable functions but result in a Fatal Error instead. Why? Regards, Philip On Mon, 7 May 2001, Johnson, Kirk wrote: Change the parens around $var to curly braces: if ($foo{$var}) print 'worked.'; Kirk -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]