php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #19463 Returning from __construct()
Submitted: 2002-09-18 01:23 UTC Modified: 2003-06-01 12:35 UTC
Votes:4
Avg. Score:4.8 ± 0.4
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:0 (0.0%)
From: scotje at wwc dot edu Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 5.0.0-dev 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: scotje at wwc dot edu
New email:
PHP Version: OS:

 

 [2002-09-18 01:23 UTC] scotje at wwc dot edu
The version is actually the 4.3.0 zend2 alpha2.

I'm wondering if it is possible to return an instance of a subclass from a constructor.  For example:

class test1
{
  class test2
  {
    function prt_test()
    {
      echo("testing!!!");
    }
  }

  function __construct()
  {
    return new test2();
  }
}
	
$test_obj = new test1();
	
$test_obj->prt_test();

?>

Let's say that I want to parent class to choose one of it's children to actually provide the functionality based on some selection criteria.

This isn't a big issue since I can just use a function seperate from the constructor to accompolish roughly the same thing.  I'm just wondering if this is something that was considered or encountered already.

Thanks...

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-09-18 09:57 UTC] sander@php.net
Reclassified.
 [2003-04-18 17:45 UTC] thekid at thekid dot de
As nested classes are no longer supported, I guess this could be closed.

thekid@friebes:~ > cat | php5
<?php
  class A { class B { } }
?>
^D
Parse error: parse error, unexpected T_CLASS, expecting T_FUNCTION in /usr/home/thekid/- on line 2
 [2003-06-01 12:35 UTC] helly@php.net
PHP5 doesn't support nested classes.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Jul 15 14:01:33 2025 UTC