Page 4 of 4

Re: PHP suggestion/rant

Posted: Sat Feb 12, 2011 3:17 pm
by Weirdan
Benjamin wrote:I don't understand what you said because you appear to be agreeing and disagreeing in the same sentence.
I thought your 'should' implied it's not the way things are in php now. With that I'm certainly disagreeing.

Re: PHP suggestion/rant

Posted: Sat Feb 12, 2011 7:01 pm
by Benjamin
Well they aren't passed by reference. It's passing a pointer. Also, it doesn't change the memory scope of anything. Passing by reference allows a variable to be modified in a different scope. Sure, you can call the methods/variables in the class, but the scope of everything in the object remains the same.

Re: PHP suggestion/rant

Posted: Sun Feb 13, 2011 12:17 pm
by Jonah Bron
Yeah, that's what Weirdan is saying. Objects are handled as "pass-reference-by-value", just like Java, Javascript, and lots of other languages. You're both saying the same thing.

Re: PHP suggestion/rant

Posted: Tue Feb 22, 2011 11:12 pm
by sankha.icraft
Thanks for nice discussion in PHP. "$" is nothing but a variable.

Re: PHP suggestion/rant

Posted: Thu Feb 24, 2011 11:50 am
by alex.barylski
That's because PHP doesn't require you to declare a variable to a datatype (only type-hinting in methods and functions).
This makes us PHP writers write ugly code, using $var as string object. PHP is just too dynamic and messy.
There are prons and cons to many approaches in software, it's a constant struggle between the lesser of two evils in language design especially cause of the audience you are targetting.

PHP is very dynamic, both it's hype and it's heel. :)

Cheers,
Alex

Re: PHP suggestion/rant

Posted: Wed Mar 02, 2011 7:42 am
by VladSun
OK, back to flame :)

Code: Select all

$a = array('a', 'b', 'c');

function changeArray($b)
{
	$b[] = 'd';
}

var_dump($a);
changeArray($a);
var_dump($a);
[text]array
0 => string 'a' (length=1)
1 => string 'b' (length=1)
2 => string 'c' (length=1)
array
0 => string 'a' (length=1)
1 => string 'b' (length=1)
2 => string 'c' (length=1)[/text]

Non scalar ...

So arrays by default are passed "by value" in PHP. Weird... Arrays in PHP are not "addresses" (like in C), and obviously they are not objects either (like in JavaScript). So, what are they :) ? Though they are "hashes" and not plain arrays, IMHO, that doesn't explain the output shown above. Even "hashes" are objects :)

Also, when I said "passing an object by value" I meant this really:

Code: Select all

function objByVal(clone $b)...
( I know that cloning an object might be very expensive ... )

I.e. when I used passing "by value" (a copy) I meant that the function was not able to change variables out of its scope. Passing "by address" (pointer/reference - doesn't matter) is the opposite.
I know these are not the precise definitions but that's what I meant indeed.

What did bother me was the "read-only" access to object properties. While it can be solved by using object cloning, there is nothing (PHP native) to protect from executing object methods inside the function, which completely breaks the "read-only-jail" (because methods might be able to "write" :) ).

In other words - passing an object "by value" is senseless.

Re: PHP suggestion/rant

Posted: Wed Mar 02, 2011 11:41 am
by Jonah Bron
I agree that anything but flat values (string, integer, boolean, etc) should be passed by reference by value. But arguably, strings, integers, and booleans could be objects...

Re: PHP suggestion/rant

Posted: Wed Mar 02, 2011 12:18 pm
by josh
@ Vlad, arrays are arrays, why make a comparison to something its not (a different construct from another language), like comparing apples & oranges. It just is what it is, there's no rule chiseled in stone that says all implementations of "array" must be pass by value vs. not.

Re: PHP suggestion/rant

Posted: Wed Mar 02, 2011 1:24 pm
by VladSun
Indeed there is a rule - large and/or unknown size data structures should be passed "by address", not "by value". Passing "by value" doubles the amount of resources needed.

An array is this kind of data structure.

PS: Still wondering - what is the type of "Array" in PHP? :)
Can't agree the answer is "arrays are arrays".

Re: PHP suggestion/rant

Posted: Wed Mar 02, 2011 3:01 pm
by John Cartwright
VladSun wrote:Indeed there is a rule - large and/or unknown size data structures should be passed "by address", not "by value". Passing "by value" doubles the amount of resources needed.

An array is this kind of data structire.

PS: Still wondering - what is the type of "Array" in PHP? :)
Can't agree the answer is "arrays are arrays".
I thought PHP arrays were basically just hash tables?

Re: PHP suggestion/rant

Posted: Wed Mar 02, 2011 3:04 pm
by VladSun
John Cartwright wrote:I thought PHP arrays were basically just hash tables?
I did agree :)
VladSun wrote:Though they are "hashes" and not plain arrays, IMHO, that doesn't explain the output shown above. Even "hashes" are objects :)
PS:

Code: Select all

class obj implements arrayaccess {
    private $container = array();
    public function __construct() {
        $this->container = array(
        );
    }
    public function offsetSet($offset, $value) {
        if (is_null($offset)) {
            $this->container[] = $value;
        } else {
            $this->container[$offset] = $value;
        }
    }
    public function offsetExists($offset) {
        return isset($this->container[$offset]);
    }
    public function offsetUnset($offset) {
        unset($this->container[$offset]);
    }
    public function offsetGet($offset) {
        return isset($this->container[$offset]) ? $this->container[$offset] : null;
    }
}

$obj = new obj;

$obj[] = 'Append 1';
$obj[] = 'Append 2';
$obj[] = 'Append 3';

function change($a)
{
	$a[] = 'Append 666';
}

change($obj);
var_dump($obj[3]);
Passed by reference. I know it's an "array object" and not a plain array, but that's the behavior I expected.

Re: PHP suggestion/rant

Posted: Wed Mar 02, 2011 5:21 pm
by Weirdan
VladSun wrote:Indeed there is a rule - large and/or unknown size data structures should be passed "by address", not "by value". Passing "by value" doubles the amount of resources needed.
You're mixing performance and semantics. Those are orthogonal concerns and it's a semantic we're talking about in this thread. That makes your argument invalid for this discussion. Assume you have an infinite memory and infinite computational resources.
PS: Still wondering - what is the type of "Array" in PHP? :)
Can't agree the answer is "arrays are arrays".
You have to because that's the only valid answer.
That's silly question to ask anyway. Compare: "What is the type of char in C? Can't agree it's char."

Re: PHP suggestion/rant

Posted: Wed Mar 02, 2011 5:42 pm
by VladSun
Weirdan wrote:You're mixing performance and semantics. Those are orthogonal concerns and it's a semantic we're talking about in this thread. That makes your argument invalid for this discussion. Assume you have an infinite memory and infinite computational resources.
"Array" and "addressing" IT terms are very closely related. Passing a list of addresses by copying values at these addresses, constructing a new list of addresses and then pass it to a function just doesnt make sense.
Weirdan wrote:You have to because that's the only valid answer.
That's silly question to ask anyway. Compare: "What is the type of char in C? Can't agree it's char."
It's not :P It's an 8bit int :) Can't agree with ASCII coding scheme?
But I can also explain what's an array in C

Array of chars: a[];

Code: Select all

char *a;
The [] operator (offset 3) a[3]

Code: Select all

*(a+=3);
The [] operator (offset 0) a[0]

Code: Select all

*(a);
!

Re: PHP suggestion/rant

Posted: Wed Mar 02, 2011 7:04 pm
by Weirdan
VladSun wrote:
Weirdan wrote:You have to because that's the only valid answer.
That's silly question to ask anyway. Compare: "What is the type of char in C? Can't agree it's char."
It's an 8bit int :)
C99 defines no 8bit ints - so char could not possibly be 8bit ints as there's no 8 bit ints defined by this standard. Some compilers (like msvc) provide int8 'type' - but they do that usually by just aliasing it to signed/unsigned char, like this: http://msdn.microsoft.com/en-us/library ... t.10).aspx
Can't agree with ASCII coding scheme?
The only requirement I see in the C language specs regarding actual byte values for characters is that digits should have ascending values. Other than that character encoding is left for implementation to decide => it's an implementation detail => irrelevant for the discussion of the language itself.
But I can also explain what's an array in C
That's too easy. Explain what the struct is. No implementation details please :lol: