note 125534 added to language.oop5.basic

From: Date: Mon, 23 Nov 2020 09:50:50 +0000
Subject: note 125534 added to language.oop5.basic
Groups: php.notes 
Request: Send a blank email to php-notes+get-215885@lists.php.net to get a copy of this message
Instantiating an object with a string variable defaults to non-namespaced scope. Given two classes in the same namespace. <?php namespace foo; class bar { public function createSubclass(string $type) { return new $type(); } ?> <?php namespace foo; class baz { } ?> <?php $barObj = new bar(); $barObj->createSubclass('baz'); // result: Uncaught Error: Class 'baz' not found ?> Change bar class to: <?php namespace foo; class bar { public function createSubclass(string $type) { type = '\\'.__NAMESPACE__.'\\'.type; return new $type(); } ?> ---- Server IP: 45.112.84.4 Probable Submitter: 185.85.3.174 (proxied: 88.109.60.247) ---- Manual Page -- http://php.net/manual/en/language.oop5.basic.php Edit -- https://master.php.net/note/edit/125534 Del: integrated -- https://master.php.net/note/delete/125534/integrated Del: useless -- https://master.php.net/note/delete/125534/useless Del: bad code -- https://master.php.net/note/delete/125534/bad+code Del: spam -- https://master.php.net/note/delete/125534/spam Del: non-english -- https://master.php.net/note/delete/125534/non-english Del: in docs -- https://master.php.net/note/delete/125534/in+docs Del: other reasons-- https://master.php.net/note/delete/125534 Reject -- https://master.php.net/note/reject/125534 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#215885) next »