|
Posted by Oli Filth on 11/15/05 21:20
Jerry Stuckle said the following on 15/11/2005 17:09:
> Rich Kucera wrote:
>
>> Why not? You never did this: $x = f1( f2( $y ) ); ?
>>
>> For example:
>>
>> $dir_path_component = array_pop( split('[/]',$dir) );
>>
>> This syntax seems second nature to me. It is now broken.
>>
>> You now have to inject a $foo variable into your syntax:
>>
>> $dir_path_component = array_pop( $foo = split('[/]',$dir) );
>>
>> Seems like other languages have solved whatever ill-effects result from
>> passing of function results to another function.
>>
>
> Sure - but not if f1() takes a reference. It works fine if f1() takes a
> value.
>
> For instance, what is supposed to happen if you do something like:
>
> function f1 (&$i) {
> $i++;
> }
>
> Java and C++ handle this by not allowing a temporary to be passed as a
> reference (compiler error) - which is correct operation, IMHO.
Well, Java doesn't do pass by reference, but it allows pass-by-value of
references to temporary objects, e.g.:
class Donkey
{
public Donkey(String name)
{
this.name = name;
}
public String name;
}
public class test
{
static Donkey foo()
{
return new Donkey("Eeyore");
}
static void bar(Donkey d)
{
System.out.println("Donkey name: " + d.name);
}
public static void main(String[] args)
{
// pass value of reference to temp object
bar(foo());
}
}
C++ gets round this if the function is expecting a const reference, e.g.:
int foo()
{
return 4;
}
void bar (const int &i) // expecting const reference
{
printf("Value: %d\n", i);
}
int main()
{
bar(foo()); // pass temp value
return 0;
}
Obviously, PHP doesn't really do type-checking, so a PHP equivalent of
const could never be enforced.
Which is a shame, as it means that one can't differentiate between
passing by reference to eliminate object copying, and passing by
reference to allow alteration of the original object.
However, even in Jerry's example, even without the benefit of const
enforcement, I don't see why PHP can't just assign to the temporary
variable - what harm would it do?
--
Oli
[Back to original message]
|