PHP suggestion/rant

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: PHP suggestion/rant

Post by VladSun »

.
Last edited by VladSun on Fri Feb 11, 2011 5:52 am, edited 1 time in total.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Zyxist
Forum Contributor
Posts: 104
Joined: Sun Jan 14, 2007 10:44 am
Location: Cracow, Poland

Re: PHP suggestion/rant

Post by Zyxist »

Yes, I read your post and I showed that even if you assume that everything is an object and we permit only passing by references, for the "scalar objects" the natural effect of "passing the value" occurs. This is exactly how Ruby works, where
*everything* (whatever you call it - scalar, object, compound, container, array ......)
is passed by reference. And despite that, despite the fact that technically everything is passed by reference, there is an effect of "passing by value" for scalar value objects that is not implemented anywhere, but occurs naturally. Check it yourself if you don't believe. So what nosense, if you have a working, real implementation of what I've written?

To the second post of you -> take a look, what I have written. There is nothing about functions here. I am perfectly sure I have mentioned a (sub)expression $foo + 50, not the entire function. And expressions ALWAYS produce some value even if it is not used anywhere. In addition, I don't know if you are aware what's the idea of proofs actually. I made some assumptions. This means that this example IS NOT PHP, even if it uses PHP syntax (just for convenience). It is some language that works according to the specified assumptions.

My point is to show that passing scalars by values, and objects by references has a very deep sense, and it is the natural property of these two kinds of types. Any other approach would be artifical and inconsistent.

PS. My real nickname is "Zyx" :). "Zyxist" is the name of my website, and sometimes I use it as a login :).
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: PHP suggestion/rant

Post by VladSun »

.
Last edited by VladSun on Fri Feb 11, 2011 5:52 am, edited 1 time in total.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: PHP suggestion/rant

Post by Jonah Bron »

I don't know, it makes sense to me. With a native type (ie integer, string, etc), it makes sense to pass it by value, because you don't care about the actual reference. You can modify it at will and it doesn't affect anything outside the method. On the other hand, you want to make sure you are working with the exact same object (ie the same memory location).
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: PHP suggestion/rant

Post by VladSun »

Jonah Bron wrote:I don't know, it makes sense to me. With a native type (ie integer, string, etc), it makes sense to pass it by value, because you don't care about the actual reference. You can modify it at will and it doesn't affect anything outside the method. On the other hand, you want to make sure you are working with the exact same object (ie the same memory location).
Why?
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: PHP suggestion/rant

Post by Jonah Bron »

Why which one?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: PHP suggestion/rant

Post by VladSun »

.
Last edited by VladSun on Fri Feb 11, 2011 5:52 am, edited 1 time in total.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: PHP suggestion/rant

Post by Jonah Bron »

But it makes sense :D . I was only saying why PHP's pass-by-reference/value system seems right to me.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: PHP suggestion/rant

Post by Weirdan »

Ok, lets try to explain this another way. I'm sure most people here know this already though.

Lets start with a somewhat counter-intuitive statement: every data type (type of a variable), including object handles, are passed to a function in exactly the same way. It's either by value (copy-on-write under the hood, but since it's not affecting behavior [only performance] it's not relevant to this discussion), denoted as:

Code: Select all

function something($arg) { // no ampersand - pass by value
  //...
}
or by reference, denoted as:

Code: Select all

function something(&$arg) { // ampersand - pass by reference
  //...
}
Now let's see what data types a variable can hold in PHP: bool, int, float, string, array, object handle, resource handle, null

That's right, objects are never stored in a variable, in an object property or array element - and therefore are never actually passed anywhere. What gets passed is an object handle and it's passed exactly like scalars.

Now you may ask, if it was passed by value (given that function argument had no ampersand) why can I change object state referenced by the variable? The question actually contains the answer: because you've got a copy of the handle - pointing, obviously, to the same object. This is the same as passing a pointer to an object in C++ - pointer gets copied, argument now references the same object original pointer referenced, if you change the pointer you're just changing it's local value - the pointer outside does not change. '->' operator in PHP has the same semantic as in C++ - dereference the pointer to the left and access property of the object it got during dereferencing, property name being the right argument of the operator.

What this implies is that Vlad already got the uniformity he wants (in regards to function arguments, anyway), we got wealth of options for getting our objects into our functions/methods, everyone is happy. Rejoice!
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: PHP suggestion/rant

Post by Jonah Bron »

<much-rejoicing />
User avatar
Zyxist
Forum Contributor
Posts: 104
Joined: Sun Jan 14, 2007 10:44 am
Location: Cracow, Poland

Re: PHP suggestion/rant

Post by Zyxist »

VladSun -> do you know, what mathematics is? Probably not really, if you call a mathematical proof a nonsense just because you don't get it. Three posts and all you've said is "I don't care what you say, it is a nonsense because I said it's a nonsense. I'm alpha and omega, so I'm right, and you are wrong". I'd like to see at least one logical argument against me, if you want to continue this discussion. I repeat once again: this is a mathematical proof. The correctness comes from logic, not your understanding it or not.

Current PHP looks just as it looks because of such approach: "hey, I came up with a new idea, let's impement it". And they implement it without thinking what they have actually written and why. Although I'm not a mathematican, I use mathematical concepts and methodologies in application design. Every time I started with a mathematical model, the final effect was always elegant, consistent, predictable and powerful at the same time. Without it, it was a lottery.

Weirdan's post gets us back to PHP, and it's true what he's written. Technically, in PHP everything is already passed by value. The object references are actual values, objects are some boxes stored somewhere in space-time :). This gives us the effect of passing objects by references, and generally, the whole behaviour is a practical implementation of the conclusions from "my" proof. And this is why it's natural.

Finally: PHP4 showed that passing objects by values is a really bad idea, and since scalars are not objects in PHP, they cannot be passed by reference by default either, because we could not use constants as function arguments then.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: PHP suggestion/rant

Post by VladSun »

Sorry about these posts here. Sorry Zyx :oops:

I promise I will never post again after 1:00am and drunk :)
In fact I do think I should not post anything in the next 2-3 weeks - quitting smoking makes me nervous :)

See you soon :)
Bye.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: PHP suggestion/rant

Post by Benjamin »

1. Stay on topic guys.
2. If you have more than 5 posts on this board, you should never, ever . out your posts. Even drinking, your only saying things you might not say otherwise.
3. Not one specific type of data should be passed or not passed by reference. All function arguments regardless of type should be handled the same unless specified otherwise. This is AKA a "quirk", which whether you like it or not causes bugs.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: PHP suggestion/rant

Post by Weirdan »

Benjamin wrote: 3. Not one specific type of data should be passed or not passed by reference. All function arguments regardless of type should be handled the same [...]
That's what we have already. There's no problem with passing objects - because objects are not being passed in the first place. Object handles, on the other hand, are passed just like strings or integers.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: PHP suggestion/rant

Post by Benjamin »

I don't understand what you said because you appear to be agreeing and disagreeing in the same sentence. Anyway, yes with objects it's the "handle" or "pointer" that is being passed, otherwise the code below would behave strangely. The line that isn't being crossed though is scope. Everything is kept in the scope it belongs in.

Code: Select all

<?php
class foo {
    private $_bar = 'a';
    
    public function setBar($n) {
        $this->_bar = $n;
    }

    public function display() {
        echo $this->_bar . "<br />\n";
   }
}

function test($class) {
    $class->setBar('b');
    unset($class);
}

$class = new foo();

test($class);

$class->display();
Post Reply