Function variables in classes

This is a discussion on Function variables in classes within the PHP General forums, part of the PHP Programming Forums category; OK guys, thanks for all your inputs. Based on your guidance, I have tested the following code with a series ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-01-1970
Paul van Haren
 
Posts: n/a
Default Re: [PHP] Re: Function variables in classes

OK guys, thanks for all your inputs.

Based on your guidance, I have tested the following code with a
series of variations:

class foobar {
function bar2 () {
echo "Yep, in bar2() right now\n";
}

public function foo2 () {
$a = "bar2"; // Experiment 0
$a(); // Fatal error
}
}

And the variations:
$a = "bar2"; // Experiment 0
$a(); // Fatal error: Call to undefined function bar2()

$a = "foobar::bar2"; // Experiment 1
$a(); // Fatal error: Call to undefined function bar2()

$a = "bar2"; // Experiment 2
eval($a."();"); // Fatal error: Call to undefined function bar2()

$a = "foobar::bar2"; // Experiment 3
eval($a."();"); // Works but far from elegant

$a = "bar2"; // Experiment 4
$this->$a(); // Works fine

$a = "bar2"; // Experiment 5
self::$a(); // Works fine

So, I have a working solution right now. But I still don't understand the
essence of the differences between experiment #1 and #4. Also, I don't
understand the need to specify the class name in experiment #3, coming
from #2. Functions bar2() and foo2() are part of the same class foobar,
and I would assume that the name 'bar2' would be in scope of the function
foo2.

BTW: I'm running PHP v5.2.0-8 build and distributed by Debian (etch1).

Thanks again and regards, Paul.
Reply With Quote
  #2 (permalink)  
Old 01-01-1970
Paul van Haren
 
Posts: n/a
Default Re: Function variables in classes

Thanks, this helps. The code now works.
In case this is truely a bug in PHP, where should I log it? Anything that
I should do to make sure that it gets followed up?

Regards, Paul
Reply With Quote
  #3 (permalink)  
Old 01-01-1970
Paul van Haren
 
Posts: n/a
Default Re: [PHP] Function variables in classes

I just did. The result is the same however....

Regards, Paul
Reply With Quote
  #4 (permalink)  
Old 01-01-1970
Paul van Haren
 
Posts: n/a
Default Function variables in classes

Hi there,

I'm trying to execute function variables. This works fine outside class
code, but gives a fatal error when run within a class. The demo code is
here:

<?php

function bar1 () {
echo "Yep, in bar1() right now\n";
}

function foo1 () {
bar1();

$a = "bar1";
print_r ($a); echo "\n";
$a();
}

class foobar {
function bar2 () {
echo "Yep, in bar2() right now\n";
}

public function foo2 () {
foobar::bar2();

$a = "foobar::bar2";
print_r ($a); echo "\n";
$a();
}
}

foo1();


$fb = new foobar ();
$fb->foo2();
?>

The error message reads:
Fatal error: Call to undefined function
foobar::bar2() in /home/paul/demo/demo.php on line 25

Is there anyone out there who can explain what's wrong in this code?

Thanks, Paul
Reply With Quote
  #5 (permalink)  
Old 11-01-2007
Ivar van der Burg
 
Posts: n/a
Default Re: [PHP] Function variables in classes

Try using parent::bar2(); instead of foobar::bar2();

Op 1-nov-2007, om 12:10 heeft Paul van Haren het volgende geschreven:

> Hi there,
>
> I'm trying to execute function variables. This works fine outside
> class
> code, but gives a fatal error when run within a class. The demo
> code is
> here:
>
> <?php
>
> function bar1 () {
> echo "Yep, in bar1() right now\n";
> }
>
> function foo1 () {
> bar1();
>
> $a = "bar1";
> print_r ($a); echo "\n";
> $a();
> }
>
> class foobar {
> function bar2 () {
> echo "Yep, in bar2() right now\n";
> }
>
> public function foo2 () {
> foobar::bar2();
>
> $a = "foobar::bar2";
> print_r ($a); echo "\n";
> $a();
> }
> }
>
> foo1();
>
>
> $fb = new foobar ();
> $fb->foo2();
> ?>
>
> The error message reads:
> Fatal error: Call to undefined function
> foobar::bar2() in /home/paul/demo/demo.php on line 25
>
> Is there anyone out there who can explain what's wrong in this code?
>
> Thanks, Paul
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>


Ivar van der Burg
Webdeveloper TROS internet & nieuwe media
ivar.van.der.burg@tros.nl





Reply With Quote
  #6 (permalink)  
Old 11-01-2007
Sebastian Hopfe
 
Posts: n/a
Default Re: Function variables in classes

It seems to be a PHP Bug, because normaly it should very well.

but you can solve this problem, by a workarround.

using "eval($a."();");" instead of "$a();" in the class.

Best regards
Sebastian


"Paul van Haren" <paul.vanharen@zonnet.nl> schrieb im Newsbeitrag
news:35.A3.03551.B24B9274@pb1.pair.com...
> Hi there,
>
> I'm trying to execute function variables. This works fine outside class
> code, but gives a fatal error when run within a class. The demo code is
> here:
>
> <?php
>
> function bar1 () {
> echo "Yep, in bar1() right now\n";
> }
>
> function foo1 () {
> bar1();
>
> $a = "bar1";
> print_r ($a); echo "\n";
> $a();
> }
>
> class foobar {
> function bar2 () {
> echo "Yep, in bar2() right now\n";
> }
>
> public function foo2 () {
> foobar::bar2();
>
> $a = "foobar::bar2";
> print_r ($a); echo "\n";
> $a();
> }
> }
>
> foo1();
>
>
> $fb = new foobar ();
> $fb->foo2();
> ?>
>
> The error message reads:
> Fatal error: Call to undefined function
> foobar::bar2() in /home/paul/demo/demo.php on line 25
>
> Is there anyone out there who can explain what's wrong in this code?
>
> Thanks, Paul

Reply With Quote
  #7 (permalink)  
Old 11-01-2007
Sebastian Hopfe
 
Posts: n/a
Default Re: Function variables in classes

I think you should log it, because it seems to be, and you found this error.

Regard
Sebastian

"Paul van Haren" <paul.vanharen@zonnet.nl> schrieb im Newsbeitrag
news:75.92.02860.345C9274@pb1.pair.com...
> Thanks, this helps. The code now works.
> In case this is truely a bug in PHP, where should I log it? Anything that
> I should do to make sure that it gets followed up?
>
> Regards, Paul

Reply With Quote
  #8 (permalink)  
Old 11-01-2007
ZeldorBlat
 
Posts: n/a
Default Re: Function variables in classes

On Nov 1, 7:10 am, paul.vanha...@zonnet.nl (Paul van Haren) wrote:
> Hi there,
>
> I'm trying to execute function variables. This works fine outside class
> code, but gives a fatal error when run within a class. The demo code is
> here:
>
> <?php
>
> function bar1 () {
> echo "Yep, in bar1() right now\n";
> }
>
> function foo1 () {
> bar1();
>
> $a = "bar1";
> print_r ($a); echo "\n";
> $a();
> }
>
> class foobar {
> function bar2 () {
> echo "Yep, in bar2() right now\n";
> }
>
> public function foo2 () {
> foobar::bar2();
>
> $a = "foobar::bar2";
> print_r ($a); echo "\n";
> $a();
> }
> }
>
> foo1();
>
> $fb = new foobar ();
> $fb->foo2();
> ?>
>
> The error message reads:
> Fatal error: Call to undefined function
> foobar::bar2() in /home/paul/demo/demo.php on line 25
>
> Is there anyone out there who can explain what's wrong in this code?
>
> Thanks, Paul


bar2() is an /instance/ method -- not a /class/ method. You should
call it as such.

You want this:

$a = "bar2";
$this->$a();

Reply With Quote
  #9 (permalink)  
Old 11-01-2007
Nathan Nobbe
 
Posts: n/a
Default Re: [PHP] Re: Function variables in classes

On 11/1/07, Sebastian Hopfe <s.hopfe@gmx.net> wrote:
>
> I think you should log it, because it seems to be, and you found this
> error.



i would not consider this a bug.
what paul is asking about is the variable function syntax in php.
http://www.php.net/manual/en/functio...-functions.php

whats happening is php is not resolving the first portion of the variable
contents
to a class name, nor is it capable of resolving the scope resolution syntax
when
dynamically evaluating a variable contents in the context of a method call.
there is no mention of such support in the manual.

consider this fragament (it will not work)
class DynamicMethodCaller {
public function memFunc() {
echo __METHOD__ . PHP_EOL;
}

public function invoker($dynamicMethod) {
$dynamicMethod();
}
}

$instance = new DynamicMethodCaller();
$instance->invoker('$this->memFunc');


now consider this revision (which works perfectly)
class DynamicMethodCaller {
public function memFunc() {
echo __METHOD__ . PHP_EOL;
}

public function invoker($dynamicMethod) {
$this->$dynamicMethod();
}
}

$instance = new DynamicMethodCaller();
$instance->invoker('memFunc');


the only difference between this fragment and the one
originally posted is the use of static member functions in the
original post. here are 2 fragments showing what works
and what doesnt when working with static class member functions

(doest work)
class DynamicMethodCaller {
static public function memFunc() {
echo __METHOD__ . PHP_EOL;
}

public function invoker($dynamicMethod) {
$dynamicMethod();
}
}

$instance = new DynamicMethodCaller();
$instance->invoker('DynamicMethodCaller::memFunc');

(works)
class DynamicMethodCaller {
public function memFunc() {
echo __METHOD__ . PHP_EOL;
}

public function invoker($dynamicMethod) {
self::$dynamicMethod();
}
}

$instance = new DynamicMethodCaller();
$instance->invoker('memFunc');

in general the use of eval() should be avoided unless absolutely necessary.
in this case it is not necessary; just use the syntax supported by the
interpreter
and youre good to go.

-nathan

Reply With Quote
  #10 (permalink)  
Old 11-01-2007
Jochem Maas
 
Posts: n/a
Default Re: [PHP] Re: Function variables in classes

Sebastian Hopfe wrote:
> I think you should log it, because it seems to be, and you found this
> error.


it's not a bug - especially because nobody has bothered to mention the php version.

I'm guessing that it's being run on a version php5, in which case the fatal error
seems correct - you have a non-static method defined and then you are trying to
call that method statically which is not allowed (IIRC whether this works or not
depends on the minor version of php5 your running - something the OO-purism gang
forced on us, actually the restriction may have been removed again - I've completely
lost track of the state of the exact OO rules in any given version of php5)

>
> Regard
> Sebastian
>
> "Paul van Haren" <paul.vanharen@zonnet.nl> schrieb im Newsbeitrag
> news:75.92.02860.345C9274@pb1.pair.com...
>> Thanks, this helps. The code now works.
>> In case this is truely a bug in PHP, where should I log it? Anything that
>> I should do to make sure that it gets followed up?
>>
>> Regards, Paul

>

Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 10:36 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0