note 126621 added to language.namespaces.dynamic

From: Date: Fri, 19 Nov 2021 05:12:04 +0000
Subject: note 126621 added to language.namespaces.dynamic
Groups: php.notes 
Request: Send a blank email to php-notes+get-217265@lists.php.net to get a copy of this message
<?php //single or double quotes with single or double backslash in dynamic namespace class. namespace Country_Name{ class Mexico{ function __construct(){ echo __METHOD__,"<br>"; } } $a = 'Country_Name\Mexico';//Country_Name\Mexico::__construct $a = "Country_Name\Mexico"; //Country_Name\Mexico::__construct $a = '\Country_Name\Mexico'; //Country_Name\Mexico::__construct $a = "\Country_Name\Mexico"; //Country_Name\Mexico::__construct $a = "\\Country_Name\\Mexico"; //Country_Name\Mexico::__construct $o = new $a; } /* if your namespace name or class name start with lowercase n then you should be alart about the use of single or double quotes with backslash */ namespace name_of_country{ class Japan{ function __construct() { echo __METHOD__,"<br>"; } } $a = 'name_of_country\Japan'; //name_of_country\Japan::__construct $a = "name_of_country\Japan"; //name_of_country\Japan::__construct $a = '\name_of_country\Japan'; //name_of_country\Japan::__construct //$a = "\name_of_country\Japan"; //Fatal error: Uncaught Error: Class ' ame_of_country\Japan' not found //In this statement "\name_of_country\Japan" means -first letter n with "\ == new line("\n). for fix it we can use double back slash or single quotes with single backslash. $a = "\\name_of_country\\Japan"; //name_of_country\Japan::__construct $o = new $a; } namespace Country_Name{ class name{ function __construct(){ echo __METHOD__,"<br>"; } } $a = 'Country_Name\name'; //Country_Name\Norway::__construct $a = "Country_Name\name"; //Country_Name\Norway::__construct $a = '\Country_Name\name'; //Country_Name\Norway::__construct //$a = "\Country_Name\name"; //Fatal error: Uncaught Error: Class '\Country_Name ame' not found //In this statement "\Country_Name\name" at class name's first letter n with "\ == new line("\n). for fix it we can use double back slash or single quotes with single backslash $a = "\\Country_Name\\name"; //Country_Name\name::__construct $o = new $a; } //"\n == new line are case insensitive so "\N could not affected ?> ---- Server IP: 45.112.84.4 Probable Submitter: 45.112.85.242 (proxied: 103.120.33.26) ---- Manual Page -- https://php.net/manual/en/language.namespaces.dynamic.php Edit -- https://main.php.net/note/edit/126621 Del: integrated -- https://main.php.net/note/delete/126621/integrated Del: useless -- https://main.php.net/note/delete/126621/useless Del: bad code -- https://main.php.net/note/delete/126621/bad+code Del: spam -- https://main.php.net/note/delete/126621/spam Del: non-english -- https://main.php.net/note/delete/126621/non-english Del: in docs -- https://main.php.net/note/delete/126621/in+docs Del: other reasons-- https://main.php.net/note/delete/126621 Reject -- https://main.php.net/note/reject/126621 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#217265) next »