php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #41623 $this in parent::__construct refere to the subclass
Submitted: 2007-06-07 10:14 UTC Modified: 2007-06-07 15:40 UTC
From: amine dot abbes at gmail dot com Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 5.2.3 OS: windows XP
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: amine dot abbes at gmail dot com
New email:
PHP Version: OS:

 

 [2007-06-07 10:14 UTC] amine dot abbes at gmail dot com
Description:
------------
When calling parent::__construct() in the subclass constructor, $this will refere to the subclass instance.

Reproduce code:
---------------
<?php
 class MyParent{
	function __construct(){
		echo (($this instanceof MyClass)?"Yes":"No") . "<br>" ;
		self::myfunction();
		$this->myfunction();
	}
	
	function myfunction(){
		echo __METHOD__ . "<br>";
	}
	
}

class MyClass extends MyParent{
	
	function __construct(){
		parent::__construct();
	}
	
	function myfunction(){
		echo __METHOD__ . "<br>";
	}
	
}

new MyClass();
?>

Expected result:
----------------
No
MyParent::myfunction
MyParent::myfunction

Actual result:
--------------
Yes
MyParent::myfunction
MyClass::myfunction

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-06-07 10:20 UTC] tony2001@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


 [2007-06-07 15:40 UTC] amine dot abbes at gmail dot com
There is an issue because the members (vars) refere to the class englobing "$this" but the functions refere to the caller class.
Please check with this code :

<?php
 class MyParent{
 	private $var = "varMyParent";
	function __construct(){
		echo "\$this is an instance of MyClass : " . (($this instanceof MyClass)?"Yes":"No") . "<br>" ;
		echo "Calling \$this->myfunction() in MyParent : ";
		$this->myfunction();
		echo "\$this->var in MyParent = " . $this->var . "<br>";
	}
	
	function myfunction(){
		//echo (($this instanceof MyClass)?"Yes":"No") . " in myfunction MyParent<br>" ;
		echo __METHOD__ . "  - \$this->var = " . $this->var . "<br>";
		
	}
}

class MyClass extends MyParent{
	private $var = "varMyClass";
	function __construct(){
		parent::__construct();
		echo "\$this->var in MyClass = " . $this->var . "<br>";
	}
	
	function myfunction(){
		echo __METHOD__ . "  -  \$this->var = " . $this->var . "<br>";
	}
}

new MyClass();
?>

Result :

$this is an instance of MyClass : Yes
Calling $this->myfunction() in MyParent : MyClass::myfunction - $this->var = varMyClass
$this->var in MyParent = varMyParent
$this->var in MyClass = varMyClass

=> conclusion :
In subclass MyClass inherited from MyParent, $this in parent::__construct() code block has diffrent visibility of variables and functions. The functions will continue to refere to MyClass and the variables will refere to MyParent.
This is ambiguous for the developers and should be fixed.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 07:01:28 2024 UTC