php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #44649 Object assignment error in __construct method
Submitted: 2008-04-06 07:14 UTC Modified: 2008-04-06 08:11 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: j dot amel83 at gmail dot com Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 5.2.5 OS: Windows MCE (Zend Core)
Private report: No CVE-ID: None
 [2008-04-06 07:14 UTC] j dot amel83 at gmail dot com
Description:
------------
I got Catchable fatal error when I want to pass an object as a __construct method args and assign it to a class property. PHP try to convert my object to string before assignment.  

Reproduce code:
---------------
<?php
require_once '../../RAPI/autoload.php';

class ActionController {
	
	/**
	 * TransactionScript Reference
	 *
	 * @var TransactionScript
	 */
	public $object;
	
	/**
	 * Method name
	 *
	 * @var String
	 */
	public $method;
	
	/**
	 * Method parameters
	 *
	 * @var Array
	 */
	public $params = array();
	
	/**
	 * Response Name
	 *
	 * @var String
	 */
	public $responseName;
	
	/**
	 * @param TransactionScript $object
	 * @param String $method
	 * @param Array $params
	 * @param String $responseName
	 */
	public function __construct(TransactionScript $object, $method, Array $params, $responseName) {
		
		$this->$object  = $object;
		$this->$method  = $method;
		$this->$params  = $params;
		$this->$responseName = $responseName;
		
	}
}

$a = new ActionController(new LongtermAlon(), 'sayHello', array(), 'sname');
echo $a->responseName;
?>

Expected result:
----------------
sname

Actual result:
--------------
Catchable fatal error: Object of class LongtermAlon could not be converted to string in C:\Inetpub\Refah\testArea\ActionController.php on line 42

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-04-06 07:33 UTC] colder@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.

$this->object = ...
 [2008-04-06 08:01 UTC] j dot amel83 at gmail dot com
I try to pass an object to __constrcut method. PHP try to convert my object to string before assignment.

$this->object = $object;

I think the code was clear.
do you mean that I can't pass an object reference to constructor of a Class in PHP.

Thanks for your consideration.
 [2008-04-06 08:11 UTC] colder@php.net
No, I mean that your code uses $this->$object = $object; which will try to get the property name from $object, which will try to get its string representation, hence the error.

You obviously want $this->object = $object; instead of $this->$object = $object;. Same for the other assignations.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sun Sep 07 19:00:02 2025 UTC