I thought your 'should' implied it's not the way things are in php now. With that I'm certainly disagreeing.Benjamin wrote:I don't understand what you said because you appear to be agreeing and disagreeing in the same sentence.
PHP suggestion/rant
Moderator: General Moderators
Re: PHP suggestion/rant
Re: PHP suggestion/rant
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.
- Jonah Bron
- DevNet Master
- Posts: 2764
- Joined: Thu Mar 15, 2007 6:28 pm
- Location: Redding, California
Re: PHP suggestion/rant
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
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
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.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.
PHP is very dynamic, both it's hype and it's heel.
Cheers,
Alex
Re: PHP suggestion/rant
OK, back to flame
[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:
( 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.
Code: Select all
$a = array('a', 'b', 'c');
function changeArray($b)
{
$b[] = 'd';
}
var_dump($a);
changeArray($a);
var_dump($a);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
Also, when I said "passing an object by value" I meant this really:
Code: Select all
function objByVal(clone $b)...
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
- Jonah Bron
- DevNet Master
- Posts: 2764
- Joined: Thu Mar 15, 2007 6:28 pm
- Location: Redding, California
Re: PHP suggestion/rant
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
@ 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
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".
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
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Re: PHP suggestion/rant
I thought PHP arrays were basically just hash tables?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".
Re: PHP suggestion/rant
I did agreeJohn Cartwright wrote:I thought PHP arrays were basically just hash tables?
PS:VladSun wrote:Though they are "hashes" and not plain arrays, IMHO, that doesn't explain the output shown above. Even "hashes" are objects
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]);There are 10 types of people in this world, those who understand binary and those who don't
Re: PHP suggestion/rant
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.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 have to because that's the only valid answer.PS: Still wondering - what is the type of "Array" in PHP?![]()
Can't agree the answer is "arrays are arrays".
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
"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'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.
It's notWeirdan 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."
But I can also explain what's an array in C
Array of chars: a[];
Code: Select all
char *a;Code: Select all
*(a+=3);Code: Select all
*(a);There are 10 types of people in this world, those who understand binary and those who don't
Re: PHP suggestion/rant
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).aspxVladSun wrote:It's an 8bit intWeirdan 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."![]()
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.Can't agree with ASCII coding scheme?
That's too easy. Explain what the struct is. No implementation details pleaseBut I can also explain what's an array in C