Strange behavior with parser/methods?

From: Date: Sun, 04 Feb 2001 21:43:20 +0000
Subject: Strange behavior with parser/methods?
Groups: php.general 
Request: Send a blank email to php-general+get-38148@lists.php.net to get a copy of this message
[script at end] Does it seem strange that although object members can store functions, they can't be used as methods? That is, we can have $TestObject->mTestMember = create_function('','echo "TestMember<br>";'); but we can't do $TestObject->mTestMember(); Can I get around this using clever placement of {}'s or ()'s? Or do I have to make two separate calls, such as $temp = $TestObject->mTestMember; $temp(); At first glance, it seems like a parser problem. But I couldn't get it to go away by placing ()'s and {}'s... Perhaps I wasn't being clever enough. Can anyone help? It seems odd that accessing members as methods wouldn't be allowed, but variable functions (http://www.php.net/manual/en/functions.variable-functions.php PHP Manual, Chap 12) would be. On the other hand, it's not safe to willy-nilly be introducing methods to a class. But then again, I can still do $TestObject->mPreviouslyUndeclaredMember = 'Huh?'; so, I don't buy this line of reasoning. Bug or language feature or other? Thanks, TSB Here's a script which illustrates this problem... <?php class TestClass { function TestMethod() { echo "TestMethod<br>"; } function Init() { $this->mTestMember = create_function('','echo "TestMember<br>";'); } var $mTestMember; } $Tc = new TestClass; //case 1: works (standard method invocation) $Tc->TestMethod(); $Tc->Init(); //case 2: doesn't work (member as method) //$Tc->mTestMember(); //doesn't work //case 3: works (two-step) $holder = $Tc->mTestMember; $holder(); ?>

« previous php.general (#38148) next »