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
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: PHP suggestion/rant

Post 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.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: PHP suggestion/rant

Post 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.
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 »

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.
sankha.icraft
Forum Newbie
Posts: 2
Joined: Tue Feb 22, 2011 7:33 am
Location: Kolkata

Re: PHP suggestion/rant

Post by sankha.icraft »

Thanks for nice discussion in PHP. "$" is nothing but a variable.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: PHP suggestion/rant

Post 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
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 »

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.
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 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...
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: PHP suggestion/rant

Post 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.
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 »

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".
Last edited by VladSun on Wed Mar 02, 2011 3:01 pm, 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
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: PHP suggestion/rant

Post 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?
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 »

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.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: PHP suggestion/rant

Post 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."
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 »

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);
!
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: PHP suggestion/rant

Post 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:
Post Reply