SitePoint Sponsor |
|
User Tag List
Results 1 to 25 of 73
-
Nov 28, 2005, 05:04 #1
- Join Date
- Mar 2004
- Location
- Russia, Penza
- Posts
- 265
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Upgrading scripts to conform to PHP 4.4 ref. implementation
Folks, is there any PHP 4.4 upgrading paper? There's very nice PHP 5.1 upgrading manual but i couldn't find anything on PHP 4.4 while its new reference implementation breaks tons of existing scripts.
Basically, all return/pass by reference calls should be changed. e.g the following code:
PHP Code:function &Foo($id) {
if(isset($arr[$id]))
return $arr[$id];
}
PHP Code:function &Foo($id) {
if(isset($arr[$id]))
return $arr[$id];
$zref = null;
return $zref;
}
PHP Code:function &Foo($id) {
if(isset($arr[$id]))
return $arr[$id];
return $zref = null;
}
PHP Code:function foo(&$arr) {
$arr[] = 3;
}
foo($arr = array(1, 2));
PHP Code:class Foo {
var $ref = 1;
function & foo() {
return $this->ref;
}
}
class Bar extends Foo {
function & foo() {
return parent :: foo();
}
}
Any other issues like those i mentioned?
-
Nov 28, 2005, 05:11 #2
- Join Date
- Nov 2004
- Location
- Ankh-Morpork
- Posts
- 12,158
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by pachanga
Originally Posted by pachanga
Originally Posted by pachanga
<edit>Or so I thought. A quick test shows that $arr actually contains three elements after the call. Odd.</edit>
Originally Posted by pachanga
Birnam wood is come to Dunsinane
-
Nov 28, 2005, 05:16 #3
- Join Date
- Mar 2004
- Location
- Russia, Penza
- Posts
- 265
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by AutisticCuckoo
-
Nov 28, 2005, 06:06 #4
- Join Date
- Apr 2004
- Location
- germany
- Posts
- 4,324
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
PHP Code:# this works
function foo(&$x) {
$x++;
}
foo($a = 1);
echo $a;
# and even this
function &bar() {
$a = 456;
return $zz = &$a;
}
echo bar();
# but this doesn't
function &baz() {
return $zz = 123;
}
echo baz();
-
Nov 28, 2005, 07:55 #5
- Join Date
- Nov 2005
- Location
- Norway
- Posts
- 26
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
That's an excellent example:
Originally Posted by stereofrog
-
Nov 28, 2005, 07:57 #6
- Join Date
- Apr 2004
- Location
- germany
- Posts
- 4,324
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Why does the first one work then?
-
Nov 28, 2005, 08:43 #7
- Join Date
- Sep 2004
- Location
- Zagreb, Croatia
- Posts
- 830
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by stereofrog
Basically, you can only reference a variable, not a value.
PHP Code://this:
return $foo = 'bar';
//is the equivalent of
$foo = 'bar';
return 'bar';
// while this
function foo(&$x) {}
foo($a = 1);
// is the equivalent of this:
function foo(&$x) {}
$a = 1;
foo($a);
-
Nov 28, 2005, 09:15 #8
- Join Date
- Jun 2004
- Location
- Copenhagen, Denmark
- Posts
- 6,157
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
So return $foo = 'bar'; doesn't return $foo, but rather returns 'bar', whereas foo($a = 1); assigns 1 to $a, and calls foo() with $a as argument ?
That's a bit confusing actually, that return doesn't evaluate the statement beforehand.
-
Nov 28, 2005, 09:16 #9
- Join Date
- Apr 2004
- Location
- germany
- Posts
- 4,324
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
@Berislav: Yeah, thanks, it doesn't explain that much. What I am looking for is a trace of logic in this stuff.
Originally Posted by BerislavLopac
PHP Code:$foo = 'bar';
return $foo;
// while this
function foo(&$x) {}
foo($a = 1);
// is the equivalent of this:
function foo(&$x) {}
$a = 1;
foo($a);
PHP Code:$a = 1;
foo(1);
-
Nov 28, 2005, 09:33 #10
- Join Date
- Mar 2004
- Location
- Russia, Penza
- Posts
- 265
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Folks, maybe it's a bug?
-
Nov 28, 2005, 09:57 #11
- Join Date
- Jun 2003
- Location
- Iowa, USA
- Posts
- 3,749
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Damn, just when I thought I understood variables and references
Code:$ php -d error_reporting=4095 -r 'function &foo() { return $x = 1; } $r =& foo(); var_dump($r);' Strict Standards: Only variable references should be returned by reference in Command line code on line 1 int(1) $ php -d error_reporting=4095 -r 'function &foo() { $x = 1; return $x; } $r =& foo(); var_dump($r);' int(1)
Code:$ php -d error_reporting=4095 -r 'function foo(&$x) { var_dump($x); } foo(1);' Fatal error: Only variables can be passed by reference in Command line code on line 1 $ php -d error_reporting=4095 -r 'function foo(&$x) { var_dump($x); } foo($x = 1);' int(1)
Last edited by sweatje; Nov 28, 2005 at 10:30. Reason: added inbound function parameter reference for comparison
Jason Sweat ZCE - jsweat_php@yahoo.com
Book: PHP Patterns
Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
Detestable (adjective): software that isn't testable.
-
Nov 28, 2005, 10:48 #12
That's why I don't even do crazy stuff like return $foo = 'bar'; The behavior is unpredictable, in the sense that it is not well documented and people expect it to do different things
Already this thread isn't very long and we have about 3 different interpretations over what it should be doing.
As I read it, it should assign "bar" to $foo and return a boolean true... I mean after-all the result of assigning a value to a variable would be successful [true], would it not?
The again, I am just guessing at what I *think* it would do, since I don't write code like that in the first place.
-
Nov 28, 2005, 11:04 #13
- Join Date
- Mar 2004
- Location
- Russia, Penza
- Posts
- 265
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
As I read it, it should assign "bar" to $foo and return a boolean true... I mean after-all the result of assigning a value to a variable would be successful [true], would it not?
The again, I am just guessing at what I *think* it would do, since I don't write code like that in the first place.
PHP Code:class Container {
function & findObject($id) {
if(isset($this->items[id]))
return $this->items[id];
else
return $zref = null;
}
}
PHP Code:class Container {
function & findObject($id) {
if(isset($this->items[id]))
return $this->items[id];
$zref = null;
return $zref;
}
}
-
Nov 28, 2005, 11:10 #14
what is wrong with this:
PHP Code:class Container {
function &findObject($id) {
$return = null;
if(isset($this->items[id])) {
$return =& $this->items[id];
}
return $return;
}
}
-
Nov 28, 2005, 11:52 #15
- Join Date
- Mar 2004
- Location
- Russia, Penza
- Posts
- 265
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by dreamscape
-
Nov 28, 2005, 12:35 #16
Originally Posted by pachanga
Relatively speaking, it doesn't take all that long to do. You've just gotta buckle down and get it over with, whatever approach you decide to take.
Consider that you've likely spent more time looking for an easy way to do it, than the amount of time it would take you to just sit down and do it
Off Topic:
I do the same thing. I will spend a good half hour searching for the remote before just walking up to the tv and switching the channel button. When we talk about stuff like this later, it really seems like irrational beahvior, but at the time, we were thinking very rationally
-
Nov 28, 2005, 12:51 #17
- Join Date
- Aug 2003
- Location
- Toronto
- Posts
- 300
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Not helpful as a work around, but perhaps helpful to explain behaviour; consider this example:
PHP Code:// throws a notice
function &baz() {
return $zz = 123;
}
echo baz();
// returns 123
function &foobar(&$zz) {
return $zz = 123;
}
echo foobar($zz=321);
IIR, some OOP languages don't allow statements to be used as expressions and in many cases, they really don't save very much typing and can hide / obscure important operations.
BTW: if you really can't stand a temporary var and don't mind the overhead of a function call, use a wrapper funciton:
PHP Code:function &SafeRet($value=null)
{
return $value;
}
function &bar()
{
return SafeRet();
}
echo bar();
Last edited by jayboots; Nov 28, 2005 at 13:36.
-
Nov 28, 2005, 14:11 #18
- Join Date
- Sep 2004
- Location
- Zagreb, Croatia
- Posts
- 830
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by stereofrog
Consider this: when you assign value 'bar' to a variable $foo, what really happens?
PHP stores variables and their values separately, that's what happens. So when you state $foo = 'bar', you put 'baz' as the value of the variable $foo; however, when you state $foo =& $baz, you put as the value of $foo the *reference*, i.e. an internal pointer to the location of the *value* of $baz.
On the other hand, the return statement -- probably for historical reasons -- work as the equivalent of chained assignments, i.e. $a = $b = $c = 1000, which creates variables $a, $b and $c and assign the same value to each of them. The only difference is that when you declare the function to return a reference, it *has* to be assigned a reference, not a value.
It's actually quite simple once you get it. Not unlike the pointers in C.
-
Nov 28, 2005, 14:14 #19
- Join Date
- Sep 2004
- Location
- Zagreb, Croatia
- Posts
- 830
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by pachanga
When your function declaration begins with a &, you *must* pass a reference to its return statement. A return in that case is an implicit =& operator -- so you can't return a literal value, as it hasn't been associated a reference yet.
One more thing: assignment operator returns nothing -- it's an operator, not a function.
And a side note: It makes no sense to return the value of a parameter that has been passed by a reference. Consider:
PHP Code:<?php
function &foo(&$bar)
{
$bar++;
return $bar;
}
$baz = 10;
$wazoo =& foo($baz);
echo $wazoo . ' = ' . $baz . '<br/>';
$baz++;
echo $wazoo . ' = ' . $baz . '<br/>';
/*
it prints:
11 = 11
12 = 12
*/
-
Nov 28, 2005, 18:13 #20
- Join Date
- Nov 2002
- Posts
- 841
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I just wanted to suggest that instead of using meaningless variable names like $return, $ret, or $zval, that it might be better to use a name with meaning.
PHP Code:class Container {
function &findObject($id) {
$object = null;
if(isset($this->items[id])) {
$object =& $this->items[id];
}
return $object;
}
}
PHP Code:return $return;
-
Nov 28, 2005, 18:21 #21
- Join Date
- Aug 2003
- Location
- Toronto
- Posts
- 300
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by Selkirk
I also agree with BerislavLopec concerning the merit of returing a reference to a parameter that was passed-by-reference. Earlier I had used that metaphor but only to demonstrate how refrences are treated differently than values in assignment. Don't Do what Donny Don't Does
-
Nov 28, 2005, 20:38 #22
Originally Posted by Selkirk
If you do HTML, you probably use DIV's alot. You know what a DIV means? It means absolutely nothing. It is a meaningless container.
So you see, there is nothing wrong with using "meaningless" names.
-
Nov 28, 2005, 21:45 #23
- Join Date
- Aug 2004
- Location
- California
- Posts
- 1,672
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by dreamscape
Originally Posted by dreamscape
Christopher
-
Nov 28, 2005, 22:06 #24
- Join Date
- Nov 2002
- Posts
- 841
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
$object has about as much meaning as you can extract in the context of a function called findObject. My point is that if your function were called findInvoice, then
PHP Code:return $invoice;
PHP Code:return $return;
-
Nov 28, 2005, 23:24 #25
I really don't know what you're going on about... the code in the thread is all sample code to demonstrate either some problem or solution. You probably wouldn't really call a class "Foo" or "Bar" or "Container" either.
Off Topic:
arborint, DIV might stand for "division" but in terms of HTML markup it is quite meaningless. There are some simple rules like it cannot be inside of a paragraph, but its actual function is meaningless on its own, unlike other tags. The same is true for SPAN as well. They are simply nothing more than generic containers, and you have to give them meaning and shape. That was simply my point there, that they are in fact quite meaningless in the markup and there is nothing wrong with that
Bookmarks