Asked  7 Months ago    Answers:  5   Viewed   38 times

I know this is covered in the php docs but I got confused with this issue .

From the php docs :

$instance = new SimpleClass();
$assigned   =  $instance;
$reference  =& $instance;
$instance->var = '$assigned will have this value';
$instance = null; // $instance and $reference become null
var_dump($instance);
var_dump($reference);
var_dump($assigned);
?>

The above example will output:

NULL
NULL
object(SimpleClass)#1 (1) {
["var"]=>
 string(30) "$assigned will have this value"
}

OK so I see that $assigned survived the original object ($instance) being assigned to null, so obviously $assigned isn't a reference but a copy of $instance.

So what is the difference between

 $assigned = $instance 

and

 $assigned = clone $instance

 Answers

33

Objects are abstract data in memory. A variable always holds a reference to this data in memory. Imagine that $foo = new Bar creates an object instance of Bar somewhere in memory, assigns it some id #42, and $foo now holds this #42 as reference to this object. Assigning this reference to other variables by reference or normally works the same as with any other values. Many variables can hold a copy of this reference, but all point to the same object.

clone explicitly creates a copy of the object itself, not just of the reference that points to the object.

$foo = new Bar;   // $foo holds a reference to an instance of Bar
$bar = $foo;      // $bar holds a copy of the reference to the instance of Bar
$baz =& $foo;     // $baz references the same reference to the instance of Bar as $foo

Just don't confuse "reference" as in =& with "reference" as in object identifier.

$blarg = clone $foo;  // the instance of Bar that $foo referenced was copied
                      // into a new instance of Bar and $blarg now holds a reference
                      // to that new instance
Wednesday, March 31, 2021
 
ChronoFish
answered 7 Months ago
34

As BoltClock mentioned there is no object literal in PHP however you can do this by simply type casting the arrays to objects:

$testArray = array(
    (object)array("name" => "John", "hobby" => "hiking"),
    (object)array("name" => "Jane", "hobby" => "dancing")
);

echo "Person 1 Name: ".$testArray[0]->name;
echo "Person 2 Hobby: ".$testArray[1]->hobby;
Sunday, August 8, 2021
 
konstantin
answered 3 Months ago
27

You can't create new objects in class property declarations. You have to use the constructor to do this:

class SinglyLinkedlistTester {
    public static $ll;

    public function __construct() {
        static::$ll = new Linklist();
    }
}

Edit: Also, you can test your files for errors without executing them using PHP's lint flag (-l):

php -l your_file.php

This will tell you whether there are syntax or parsing errors in your file (in this case, it was a parse error).

Sunday, August 22, 2021
 
Otiel
answered 2 Months ago
26

This is an optimisation called string pooling in which compile-time constant Strings (aka known to be identical at compile time) can be set such that they really are the same object in memory (saving space for one of the most used types of object). Or in the words of the docs;

"All literal strings and string-valued constant expressions are interned."

Note that this only applies to Strings that are defined at compile time, so the following truly would print false.

String a="Hello";
String b=new String("Hello");
System.out.println(a==b); //prints false because a new string was forced

or

String a="Hello";
String b1="He";
String b2="llo";
String b=b1+b2;

System.out.println(a==b); //prints false because b wasn't know to be "Hello" at compile time so could not use string pooling

N.B. It is possible to cause the second snippet to print true by making b1 and b2 final, allowing b1+b2 to be known at compile time. All in all you need to be very careful and treat string==string with considerable respect, in the vast majority of cases you want string.equals(string) in which this behaviour does not exist.

Wednesday, September 1, 2021
 
KHM
answered 2 Months ago
KHM
40

It will be destructed (unloaded from memory) at the end of the page load, or if you unset all references to it earlier. You will not have to destroy it manually since PHP always cleans up all memory at the end of the script.

In fact, you should never call __destruct yourself. Use unset to unset the reference to an object when you want to destroy it. __destruct will in fact not destroy your object, it's just a function that will get called automatically by PHP just before the destruction so you get a chance to clean up before it's destroyed. You can call __destruct how many times as you want without getting your memory back.

If, however, you've saved the object to a session variable, it will "sleep" rather than be destroyed. See the manual for __sleep. It will still be unloaded from memory (and saved to disk) of course since PHP doesn't hold anything in memory between scripts.

Tuesday, October 12, 2021
 
supermitch
answered 2 Weeks ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :