php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #35728 constructors with a return by reference cause a notice to be thrown
Submitted: 2005-12-19 00:18 UTC Modified: 2005-12-19 14:49 UTC
From: t dot isler at bbn dot de Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.1.1 OS:
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 you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: t dot isler at bbn dot de
New email:
PHP Version: OS:

 

 [2005-12-19 00:18 UTC] t dot isler at bbn dot de
Description:
------------
I don't know wether you want to consider the following a bug, but as far as my understanding of oop is concerned, the behavior of php 4.4 and 5.1 is "unusual" when comes to constructors with a return by reference.

Reproduce code:
---------------
<?php
  class firstObject { function & __construct() {} }
  class secondObject { function & __construct() {return $this;} }
  $firstObject =& new firstObject();
  $secondObject =& new secondObject();
?>

Expected result:
----------------
The first constructor should pass without a problem, the second should throw an error.

Actual result:
--------------
The first constructor throws a notice, the second passes without any problem.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-12-19 00:32 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

That's exactly what you get with this:
<?php
function &foo() {}
$test = & foo();
?>
And this is expected behaviour (because your code is obviously wrong).
Also, if you set error_reporting to E_ALL | E_STRICT, you'll see even more error messages telling you about your code.
 [2005-12-19 14:29 UTC] t dot isler at bbn dot de
Even if I remove the assignments by reference it does not change the problem. Next to fact that I do not see the sense of your code example unless you wish to imply that constructors in PHP do not differ from regular functions. As far as OOP is concerned a constructor has an implicit return ($this) and languages such as C++ or Java will throw an error, if a constructor includes an explicit return.
 [2005-12-19 14:49 UTC] tony2001@php.net
>Even if I remove the assignments by reference it does not
> change the problem.
Yes it does.

>Next to fact that I do not see the sense of your code example
That's exactly what I wanted to demonstrate.
There is no point in returning by reference from constructor, since it doesn't return anything at all.

 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sun Sep 14 13:00:01 2025 UTC