#50380 [Bgs]: segfaults: non static class calls inside of objects will cause a fake __call

From: Date: Tue, 08 Dec 2009 16:33:13 +0000
Subject: #50380 [Bgs]: segfaults: non static class calls inside of objects will cause a fake __call
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-145533@lists.php.net to get a copy of this message
 ID:               50380
 User updated by:  dkr at mindwerk dot de
-Summary:          non static class calls inside of objects will cause a
                   fake __call
 Reported By:      dkr at mindwerk dot de
 Status:           Bogus
 Bug Type:         Class/Object related
 Operating System: linux 2.6.21
 PHP Version:      5.2.11
 New Comment:

No, that is NOT BOGUS i think, there is something went wrong, and it
took me quite a while finding the error in the app (using strace etc),
and quite a while yet to reproduce this behavior with a small code
snippet that will SEGFAULT. 

Note: Currently only tested on 5.2.11 and 5.2.0-8+etch15

Reproduce code:
---------------

<?php
class Foo
{
	function __call($f,$a)
	{
		echo("__call activated on class: ".__CLASS__.PHP_EOL);
	}
	function write($text) {
		echo( 'Foo: '.$text.PHP_EOL );
	}
	function debug($text) {
		return call_user_func_array(
			array(self,'write'),
			array($text,1)
		);
	}
}

class Bar {
	function __construct()
	{
		Foo::debug("foobar");
	}
}

class Baz {
	function __construct()
	{
		Foo::debug("foobar");
	}
	// here he dies...
	function __call($f,$a)
	{
		echo("__call activated on class: ".__CLASS__.PHP_EOL);
	}
}

$bar = new Bar();
$baz = new Baz(); // will cause php to die
?>

Expected result:
----------------
1. Calling non-static function Foo::debug() in Bar::__construct() 
a) should throw an function not found error while "self::" is used, as
the engine does a lookup in Bar and does not find any function.
b) should not make the usage of Foo::__call() as it does not interact
in the scope of Class Foo finally?

2. Calling Foo::debug() in Baz::__construct() in Baz::__construct()
a) should use __call as it is defined in Baz, and should rely on the
scope of this class and not the non static called class Foo.. Uncomment
Foo::__call to let the whole thing get more weird, as the text
"Baz::foobar" is normally echoed if Baz::__call is only defined there
(only defined!)
b) should never make the usage of Foo::call() as it does not interact
in the scope of Class Foo finally?

Actual result:
--------------
dkr@*:~$ php test.php
__call activated on class: Foo
Segmentation fault
dkr@*:~$

Actual result without Foo::__call():
------------------------------------
dkr@*:~$ php phptest.php
PHP Warning:  call_user_func_array(): Unable to call self::write() in *
on line *
Foo: foobar
dkr@*:~$


Previous Comments:
------------------------------------------------------------------------

[2009-12-04 19:02:39] dkr at mindwerk dot de

If i uncomment Bar::__call, i get the text "foobar" and not "__called!"
or any other error. That is how 5.2.11 (not tried another version)
handles it here currently.. I will try others later.

So, if self:: does rely on the scope of the object we came from
(because we have NOT defined the function as static), Foo::__call should
never be called if self:: or $this is used in Foo, or am i wrong?

It is something like PHP uses Bar::__call if it exists and if not
Foo::__call(), hence we are not extending the class...

Output if Bar::__call commented: "foobar"
Output if Bar::__call uncommented: "__called!"

------------------------------------------------------------------------

[2009-12-04 09:23:35] jani@php.net

Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to
report
a bug at http://bugs.php.net/how-to-report.php

If you comment out the __call magic you'll get pretty clear error
message why and what goes wrong.

------------------------------------------------------------------------

[2009-12-04 09:11:12] dkr at mindwerk dot de

Description:
------------
I don't really understand the following situation.. Made some comments
in the code to explain it. There is something wrong with class and
Object scopes: calling a non-static function of another class inside an
object will cause __call (in dependence of existence in the other class)
to different behaviors when using self:: instead of __CLASS__ in the
non-static method.


Reproduce code:
---------------
<?php
class Foo
{
	function __call($f,$a)
	{
		die("__called!\n");
	}
	function write($text) {
		echo($text);
	}
	// defining debug as static will cause
	// the whole thing to work properly
	function debug($text) {
		return call_user_func_array(
			// "self" acts like making a lookup in
			// class Bar, fails and then runs the
			// magic method in this class, but why?
			// i dont have extended the Bar class...
			// __CLASS__ will work, but dont work for
			// extended classes, as self should do it
			array(self,'write'),
			array($text,1)
		);
	}
}

class Bar {
	function __construct()
	{
		Foo::debug("foobar\n");
	}
	
	// uncomment the following to make Foo::debug()
	// throw the text foobar, ehm? note that we do NOT
	// echo any content here... that is something like
	// inheritance it should not do? will echo "foobar"..
	
	/*
	function __call($f,$a)
	{
	}
	*/
}

$bar = new Bar(); // will cause php to die


Expected result:
----------------
foobar

Actual result:
--------------
__called!


------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=50380&edit=1



Thread (8 messages)

« previous php.bugs (#145533) next »