Page 3 of 4
Re: PHP suggestion/rant
Posted: Thu Feb 10, 2011 4:25 pm
by VladSun
.
Re: PHP suggestion/rant
Posted: Thu Feb 10, 2011 4:46 pm
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

.
Re: PHP suggestion/rant
Posted: Thu Feb 10, 2011 5:07 pm
by VladSun
.
Re: PHP suggestion/rant
Posted: Thu Feb 10, 2011 6:11 pm
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).
Re: PHP suggestion/rant
Posted: Thu Feb 10, 2011 6:18 pm
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?
Re: PHP suggestion/rant
Posted: Thu Feb 10, 2011 6:20 pm
by Jonah Bron
Why which one?
Re: PHP suggestion/rant
Posted: Thu Feb 10, 2011 6:43 pm
by VladSun
.
Re: PHP suggestion/rant
Posted: Thu Feb 10, 2011 7:17 pm
by Jonah Bron
But it makes sense

. I was only saying why PHP's pass-by-reference/value system seems right to me.
Re: PHP suggestion/rant
Posted: Thu Feb 10, 2011 8:35 pm
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!
Re: PHP suggestion/rant
Posted: Thu Feb 10, 2011 9:00 pm
by Jonah Bron
<much-rejoicing />
Re: PHP suggestion/rant
Posted: Fri Feb 11, 2011 1:52 am
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.
Re: PHP suggestion/rant
Posted: Fri Feb 11, 2011 5:54 am
by VladSun
Sorry about these posts here. Sorry Zyx
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.
Re: PHP suggestion/rant
Posted: Fri Feb 11, 2011 9:21 am
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.
Re: PHP suggestion/rant
Posted: Fri Feb 11, 2011 7:37 pm
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.
Re: PHP suggestion/rant
Posted: Sat Feb 12, 2011 12:32 pm
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();