php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #52494 description of keyword "new"
Submitted: 2010-07-30 02:39 UTC Modified: 2010-08-15 09:40 UTC
From: delphists at apollo dot lv Assigned: degeberg (profile)
Status: Closed Package: Documentation problem
PHP Version: 5.3.3 OS:
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: delphists at apollo dot lv
New email:
PHP Version: OS:

 

 [2010-07-30 02:39 UTC] delphists at apollo dot lv
Description:
------------
URL I'm referring to is http://www.php.net/manual/en/language.oop5.basic.php#language.oop5.basic.new
1) Documentation says that "To create an instance of a class, a new object must be created and assigned to a variable", though it doesn't really have to be assigned to a variable.
2) There is an example with storing class name in a variable and using this variable to create an object, i.e., $instance = new $className(), though this does not always work as expected with namespaced classes (I'm not sure if this is a "documentation problem" or a bug). It would be worth noting that namespaced classes need a full name to be stored in variable, even if it is being used the same namespaced. See a test script for an example.

Test script:
---------------
<?php
	namespace MyNamespace {
		class MyClass {
			function __construct() {
				echo __METHOD__, "\n";
			}
		}
		$obj = new MyClass(); // works
		$obj = new \MyNamespace\MyClass(); // works
		$obj = new MyNamespace\MyClass(); // does not work, resolves as MyNamespace\MyNamespace\MyClass
		$className = '\\MyNamespace\\MyClass';
		$obj = new $className(); // works, as $className includues namespace
		$className = 'MyNamespace\\MyClass';
		$obj = new $className(); // still works, as it resolves as <global namespace>\MyNamespace\MyClass
		$className = 'MyClass';
		$obj = new $className(); // does not work
	}


Expected result:
----------------
MyNamespace\MyClass::__construct
MyNamespace\MyClass::__construct
PHP Fatal error
MyNamespace\MyClass::__construct
MyNamespace\MyClass::__construct
PHP Fatal error


Actual result:
--------------
MyNamespace\MyClass::__construct
MyNamespace\MyClass::__construct
PHP Fatal error
MyNamespace\MyClass::__construct
PHP Fatal error
MyNamespace\MyClass::__construct

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-08-15 09:39 UTC] degeberg@php.net
Automatic comment from SVN on behalf of degeberg
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=302259
Log: Fixed PHP bug #52494 (description of keyword &quot;new&quot;)
 [2010-08-15 09:40 UTC] degeberg@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: degeberg
 [2010-08-15 09:40 UTC] degeberg@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.


 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Thu Oct 08 12:00:01 2026 UTC