RE: [PHP] PHP4 to PHP5 migration with E_STRICT

2010-12-08 Thread Tommy Pham
> -Original Message-
> From: Tom Robinson [mailto:tom.robin...@motec.com.au]
> Sent: Tuesday, December 07, 2010 4:03 PM
> To: php-general@lists.php.net
> Subject: [PHP] PHP4 to PHP5 migration with E_STRICT
> 
> Hi,
> 
> I'm migrating a web application written for PHP4 to PHP5. I've turned on
> E_STRICT to have a detailed look at all the code thoroughly. I have a
number
> of 'Notices' and 'Strict Standards' messages appearing now.
> 
> I don't consider myself a PHP guru by any means so I'm seeking help with
> understanding these messages and how to fix the code.
> 
> One of the messages is:
> 
> "PHP Strict Standards:  Only variables should be assigned by reference in"
> 
> This is for a class SPControlPanel with a method
> 
> function getContentTypes(&$db)
> {
> $tabledata = array();
> 
> $sql = "select
> contenttype.*
> from
> contenttype";
> 
> return $db->queryAll($sql, true);
> }
> 
> The warning is for this segment of code below which is another method in
> the same class (marked with comment // this line):
> 
> function getCategoryTypes(&$db) {
> $tabledata = array();
> 
> $myContentTypes = &SPControlPanel::getContentTypes($db); // this
> line
> foreach ($myContentTypes as $key => $data) {
> if ($data['iscategory']) $tabledata[] = $data['contenttype'];
> }
> 
> return $tabledata;
> }
> 
> There are many more methods making assignments in a similar way in this
> class and in other classes throughout the code.
> 
> I'm not sure of the best way to re-code this to resolve the E_STRICT
warning.
> 
> Any help is much appreciated.
> 
> Regards,
> 
> Tom
> 

IIRC, objects are passed by referenced in PHP5.  There's no need to '&' by
the object variable.  Remove the & and do a var_dump and you'll see it
refers the same class object #.

Regards,
Tommy




-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] PHP4 to PHP5 migration with E_STRICT

2010-12-08 Thread Richard Quadling
On 8 December 2010 01:12, Tom Robinson  wrote:
> Thanks David.
>
> If my understanding is correct, then:
>
> SPControlPanel::getContentTypes($db);
>
> is a reference to a static instantiation of the class. If so, then it
> must be syntactically something like when using 'new' (which returns a
> reference) so there's no need to apply the & operator. Am I on the right
> track?
>
> I have some more PHP4 code in the application which caused: "PHP
> Notice:  Only variable references should be returned by reference". I
> have fixed it like this:
>
> <       function & _getTable($cached=true)
> <       {
> <               return new TableClass($this->_getTableName());
> ---
>>       function & _getTable($cached=true) {
>>               $temp = new TableClass($this->_getTableName());
>>               return $temp;
>
> Is this acceptable?
>
> I'll have to come back to the $db issue - it's not issuing a warning so
> I'll leave it alone until I've tidied up all the other issues.
>
> Regards,
>
> Tom

I'd go with ...

function _getTable($cached=true) {
  return new TableClass($this->_getTableName());
}



-- 
Richard Quadling
Twitter : EE : Zend
@RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] PHP4 to PHP5 migration with E_STRICT

2010-12-07 Thread Tom Robinson
Thanks David.

If my understanding is correct, then:

SPControlPanel::getContentTypes($db);

is a reference to a static instantiation of the class. If so, then it
must be syntactically something like when using 'new' (which returns a
reference) so there's no need to apply the & operator. Am I on the right
track?

I have some more PHP4 code in the application which caused: "PHP
Notice:  Only variable references should be returned by reference". I
have fixed it like this:

<   function & _getTable($cached=true)
<   {
<   return new TableClass($this->_getTableName());
---
>   function & _getTable($cached=true) {
>   $temp = new TableClass($this->_getTableName());
>   return $temp;

Is this acceptable?

I'll have to come back to the $db issue - it's not issuing a warning so
I'll leave it alone until I've tidied up all the other issues.

Regards,

Tom

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] PHP4 to PHP5 migration with E_STRICT

2010-12-07 Thread David Harkness
In getCategoryTypes() you're assigning a reference to the return value of
getContentTypes(), and PHP doesn't like that. You can return a reference to
a variable from a function, but taking the reference of a *value* is
meaningless since you can only create references to variables. Just remove
the & on that marked line to get rid of the warning.

Also, I notice that both of those methods (and I assume many more) take a
reference in $db. Note that all objects are assigned by reference in PHP 5,
and thus the & here is unnecessary since you are passing an object to these
methods and aren't changing the reference inside the method. Note that PHP
won't issue a warning for these since it is acceptable and you might want to
point the reference to a new object inside the function. You aren't doing
that here, so the & is superfluous and misleading.

David


[PHP] PHP4 to PHP5 migration with E_STRICT

2010-12-07 Thread Tom Robinson
Hi,

I'm migrating a web application written for PHP4 to PHP5. I've turned on
E_STRICT to have a detailed look at all the code thoroughly. I have a
number of 'Notices' and 'Strict Standards' messages appearing now.

I don't consider myself a PHP guru by any means so I'm seeking help with
understanding these messages and how to fix the code.

One of the messages is:

"PHP Strict Standards:  Only variables should be assigned by reference in"

This is for a class SPControlPanel with a method

function getContentTypes(&$db)
{
$tabledata = array();

$sql = "select
contenttype.*
from
contenttype";

return $db->queryAll($sql, true);
}

The warning is for this segment of code below which is another method in
the same class (marked with comment // this line):

function getCategoryTypes(&$db) {
$tabledata = array();

$myContentTypes = &SPControlPanel::getContentTypes($db); // this
line
foreach ($myContentTypes as $key => $data) {
if ($data['iscategory']) $tabledata[] = $data['contenttype'];
}

return $tabledata;
}

There are many more methods making assignments in a similar way in this
class and in other classes throughout the code.

I'm not sure of the best way to re-code this to resolve the E_STRICT
warning.

Any help is much appreciated.

Regards,

Tom


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php