Are there pointers in php




















But if you try to change any of these variables, PHP will make a copy of the value, and then changes the variable. If you actually want them both to point to the same value, then use this:. At first I have to say that PHP is really powerful language, knowing there is a construct named "eval", so you can create your PHP code while running it!

Note: Other superglobals can do the same trick in smaller scales. Reference are so close to pointers, but you may want to check this link for more clarification. That syntax is a way of accessing a class member. PHP does not have pointers, but it does have references. When working with objects, you generally pass by reference rather than by value - so in some ways this operates like a pointer, but is generally completely transparent.

Following is one of the example. PHP passes Arrays and Objects by reference pointers. If you want to pass a normal variable Ex. You can even create a pointer to this pointer variable. Serializing it will break the pointer, however. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Are there pointers in php?

Ask Question. Asked 12 years, 7 months ago. Active 6 years, 2 months ago. I can now take it for granted and see clearly that your are talking about "A" objects and their references. But it still reminds me too much of cloning and object comparisons, which to me borders on machine-like programming and not object-oriented programming, which is a totally different way to think. The use of clone may get you the behavior you expect when passing an object to a function, as shown below using DateTime objects as examples.

I am planning to serialize and unserialize objects as a means of storage, and my application can conveniently group large numbers of objects inside of a single object to serialize. However, this presented some questions that I needed to answer: Let's say the parent object I plan to serialize is "A" and the objects I store in it will be A a-z. If I pass A b to A c , this happens by reference. So if A c takes actions that effect the values of A b , this will also update the original A b stored in A.

Will A c have a new unique copy of A b , or will it still reference the A b stored in A? The answer is, PHP 5. This behaviour is the same used in Java, where indeed there is no notion of passing arguments by reference. On the other hand, in PHP you can pass a value by reference in PHP we refer to references as "aliases" , and this poses a threat if you are not aware of what you are really doing.

The two variables end pointing to the same object, and the other one is lost. To sum up is a good practice NOT using aliasing when handling PHP5 objects, unless your are really really sure of what you are doing. I've only tested this on 5. It was written for PHP 4 but it still applies. The only difference is in how PHP 5 handles objects: passing object variables by value only copies an internal pointer to the object.

Objects in PHP 5 are only ever duplicated if you explicitly use the clone keyword. Hopefully detach, or something like it, will become a language construct in the future. Moral: use unset when it is called for.

Re-using variables which where references before, without unsetting them first, leads to unexpected behaviour. You should have in mind that php4 keep assigned variables "automagically" referenced until they are overwritten. So the variable copy is not executed on assignment, but on modification. I think that this should be in the manual, because it can lead to confusion. I don't know if this is a bug I'm using PHP 5.

I had a for-loop that was incredibly slow and it took me some time to find out that most of the time was wasted with the function sizeof at every loop, and even more time I spent finding out that this problem it must be somehow related to the fact, that I used a reference of the array. The difference is inproportional smaller when the array size is reduced: When using loops the first function was running for 1 second, when using it wasn't even done after 30 Seconds.

I ran into a bit of a problem recently, with an array copy resulting in a reference copy of one of the elements instead of a clone. There was supposed to be some documentation on this behaviour on this page: "Due to peculiarities of the internal workings of PHP, if a reference is made to a single element of an array and then the array is copied, whether by assignment or when passed by value in a function call, the reference is copied as part of the array.

This means that changes to any such elements in either array will be duplicated in the other array and in the other references , even if the arrays have different scopes e. Elements that did not have references at the time of the copy, as well as references assigned to those other elements after the copy of the array, will behave normally i. The comments section seem to be a proper place for this, though. I must say that it has been rather confusing following all of the explanations of PHP references, especially since I've worked a lot with C pointers.

In fact, a C pointer version of this example shown below would behave exactly the same way it would not modify what bar references as the PHP reference version. It seems like PHP has problems with references, like that it can't work properly with circular references or free properly structure with more references.

In python everything is a reference, but strings are not mutable. So test is holding a reference to "test". If you assign "this should change" to test you just change it to another reference.

But your clients still have the reference to "test". Or shorter: It does not work that way in python! Now your clients hold a reference to the dictionary.

If you update the dictionary like this, your clients will see the changes:. PHP pointers vs references.



0コメント

  • 1000 / 1000