Re: Re: Function variables in classes

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Paul van Haren wrote:
> 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.

your assumptions and php's reality differ. symbol names resolution is never
tired in the class scope.

	$a = "foobar::bar2";
	$a();

this is trying to call a function called "foobar::bar2", which given that
you cant do (parse error):

	function foobar::bar2() {}

whatever munged error message you get regarding 'bar2()' not existing,
the fact remains that 'variable' function name functionality has no concept
of class scope, the T_PAAMAYIM_NEKUDOTAYIM is not recognized. never has been,
probably never will be.

	class foo {
		function bar1() {
			$a = "foo::bar2";			
			call_user_func(explode("::",$a));
		}

		static function bar2() {
			echo __METHOD__,"\n";
		}
	}

	$foo = new foo; $foo->bar1();


having no idea what it is that your actually trying to achieve, it's hard
to tell whether any percieved limitation is justified.

you might consider taking a look at reflection:

	http://nl2.php.net/reflection


> 
> BTW: I'm running PHP v5.2.0-8 build and distributed by Debian (etch1).
> 
> Thanks again and regards, Paul.
> 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux