This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 7 years ago.
What's the purpose of using backslash \ when we create an object in PHP?
$iter = new \ArrayIterator($arr);
It's used to create a new object of a fully qualified class. Say, you're code is in the namespace "Namespace1":
namespace Namespace1;
$iter = new ArrayIterator();
would be resolved as Namespace1\ArrayIterator(); and
$iter = new \ArrayIterator();
would be resolved as ArrayIterator();
See: http://php.net/manual/de/language.namespaces.basics.php for more infos about namespaces.
Related
This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 6 years ago.
$channel =<<<_XML_;
what is meaning of above statement ?
Is XML predefined variable ?
What is the meaning of <<<
This syntax is called a heredoc. It's very convenient to enter long (usually multiline) strings without having to mess around with escaping etc.
This question already has answers here:
Problems with PHP namespaces and built-in classes, how to fix?
(3 answers)
Closed 7 years ago.
I tried, to run the following code, on PHP v5.3.13, but still getting the class not found error:
$tZone = new DateTimeZone("Europe/Amsterdam");
How can I use DateTimeZone on 5.3.13?
Try this:
$tZone = new \DateTimeZone("Europe/Amsterdam");
This question already has answers here:
simple xml add namespaced child
(2 answers)
Closed 5 years ago.
I will have to produce this XML using simpleXML in php:
<fr:program name="fundref">
<fr:assertion name="funder_name">ABC Inc.
<fr:assertion name="funder_identifier">http://dx.doi.org/10.13039/xxxxxxxxxx</fr:assertion>
</fr:assertion>
<fr:assertion name="award_number">BXDFSDS</fr:assertion>
</fr:program>
I tried:
$fundRef = $myXML->addChild('fr', '', 'program');
But this is creating:
<fr xmlns="program" name="fundref">
Thank you.
You need to determine namespace like this
$myXML->addChild('fr:program', '', 'http://ololo.com/ns/1.0');
This should help PHP's SimpleXML: How to use colons in names
This question already has answers here:
In PHP, can you instantiate an object and call a method on the same line?
(9 answers)
Closed 8 years ago.
I've an object which returns a string and I would like instanciate another object with the string returned, why in PHP I can't instanciate this way ?
For Example:
// getController() returns a string name controller
$c = new $this->router->getController() ;
// I have to do this way:
$controller = $this->router->getController() ;
$c = new $controller() ;
Thank you for your help.
It's a syntax thing. I think I remember seeing an RFC to allow more dynamic class instantiation, but assigning to a variable first gets the job done, no?
This question already has answers here:
Instantiate a class with or without parentheses? [duplicate]
(3 answers)
Closed 9 years ago.
I'm learning the advance concepts of php. What does new stdClass do? I know what new stdClass(); does.
For eg) <?php new stdClass(); ? > creates a new object. Does <?php new stdClass; ? > do the same thing? Notice there's no parentheses. Does that make a difference? I can't find documentation on it for php manual.
There is no difference. PHP lets you omit the parentheses if you're not passing arguments to the constructor.