Re: [PHP] Variable functions within an object

2004-07-30 Thread Julio Sergio Santana

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:

function foo() {
   echo "In foo()\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()
Thank you again,
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

2004-07-30 Thread Curt Zirzow
* 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

2004-07-30 Thread 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).

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

2004-07-29 Thread Jason Davidson
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:
>  function foo() {
> echo "In foo()\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:
> 
>  
> function foo() {
> echo "In foo()\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
> 
> 
> Fatal error:  Call to undefined function:  fname() in - on
> line 14
> 
> 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 within an object

2004-07-29 Thread Curt Zirzow
* 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



[PHP] Variable functions within an object

2004-07-29 Thread Julio Sergio Santana
I need to record the names of functions, and then use them later.
Recently I found the following example within the on-line documentation:
\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:


function foo() {
   echo "In foo()\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

Fatal error:  Call to undefined function:  fname() in - on 
line 14

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