Asked  2 Years ago    Answers:  5   Viewed   74 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

1

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
Sunday, September 18, 2022
4

The function json_decode does not return a string.

Have a look at the documentation for the function.

http://fr2.php.net/function.json-decode.php

The problem is that you're trying to echo the object. The following code is faulty.

echo $issue_json;

You can use var_dump to see the structure of the object returned from json_decode.

var_dump($issue_json);
Wednesday, September 28, 2022
 
o.d
 
o.d
3

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).

Monday, October 24, 2022
 
siby
 
4

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.

Monday, August 29, 2022
 
4

The easiest way of doing it would be to serialize your class to string.

$string = serialize($cat);

http://php.net/manual/en/language.oop5.serialization.php

But this way you will create lots of overhead in your database.

The best way to do it would be to have the relevant fields in your table and just save the corresponding values. You can easily create a function which reads / saves your class to a table. This would make your data much more portable and exchangeable.

Thursday, August 11, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 

Browse Other Code Languages