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.
Related
This question already has answers here:
Where do we use the object operator "->" in PHP?
(6 answers)
Closed 3 years ago.
$blabla->blab, just wondering what this means something to do with an object going into another?
Sorry for being a bit vague .
The arrow-like symbol has nothing to do with "an object going into another".
The syntax $blabla->blab means a call to attribute $blab in the object $blabla.
The syntax $blabla->blab() means a call to method blab() in the object $blabla.
Please read the Basics in PHP documentations.
This question already has answers here:
How do I create a copy of an object in PHP?
(9 answers)
Closed 6 years ago.
In PHP objects are automatically passed by reference:
$obj1 = new stdClass();
$obj1->foo = 'bar';
$obj2 = $obj1;
$obj2->foo = 'OOF';
var_dump($obj1->foo); // OOF
Is there an elegant way to copy that variable and NOT refer to the original variable? I want to store a copy of an object and then modify it without effecting the original. Thanks.
You can clone the object:
$obj2 = clone $obj1;
Note that $obj2 will be a shallow copy of $obj1. As stated in the PHP manual:
When an object is cloned, PHP 5 will perform a shallow copy of all of
the object's properties. Any properties that are references to other
variables will remain references.
You can override the __clone() method to manually clone any subobjects if you wish.
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.
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:
Closed 12 years ago.
Possible Duplicate:
Reference - What does this symbol mean in PHP?
I've been programming PHP for over 5 years and I've just came across something I have never seen whilst creating a wordpress theme.
$images =& get_children( 'post_type=attachment&post_mime_type=image' );
What does $images =& do? It's the =& I'm concerned about. I have a feeling it's bitwise but I wouldn't understand what it's doing even if it was.
Any help?
Assigns a value by reference
http://www.php.net/manual/en/language.operators.assignment.php
Assignment by reference means that
both variables end up pointing at the
same data, and nothing is copied
anywhere.
=& is the assignment by reference operator.
You can find out more about references here: http://php.net/manual/en/language.references.php
It's an assign by reference.
http://php.net/manual/en/language.references.pass.php
As opposed to a normal pass by value assignment.