...

View Full Version : PHP array error



bngari
05-27-2011, 01:10 PM
Hi, a website i'm working on is having a problem. It is giving an error like
Warning: Parameter 2 to frontpage() expected to be a reference, value given in /usr/www/users/netwas/includes/Cache/Lite/Function.php on line 101

what can I do to rectify it? website address is http://netwas.org. Hlp will be appreciated.

tomharto
05-27-2011, 01:12 PM
What the code causing the error? Line 101 on Functions.php

bngari
05-27-2011, 01:56 PM
<?php

/**
* This class extends Cache_Lite and can be used to cache the result and output of functions/methods
*
* This class is completly inspired from Sebastian Bergmann's
* PEAR/Cache_Function class. This is only an adaptation to
* Cache_Lite
*
* There are some examples in the 'docs/examples' file
* Technical choices are described in the 'docs/technical' file
*
* @package Cache_Lite
* @version $Id: Function.php 49 2005-09-15 02:55:27Z rhuk $
* @author Sebastian BERGMANN <sb@sebastian-bergmann.de>
* @author Fabien MARTY <fab@php.net>
*/

// no direct access
defined( '_VALID_MOS' ) or die( 'Restricted access' );

require_once( $mosConfig_absolute_path . '/includes/Cache/Lite.php' );

class Cache_Lite_Function extends Cache_Lite
{

// --- Private properties ---

/**
* Default cache group for function caching
*
* @var string $_defaultGroup
*/
var $_defaultGroup = 'Cache_Lite_Function';

// --- Public methods ----

/**
* Constructor
*
* $options is an assoc. To have a look at availables options,
* see the constructor of the Cache_Lite class in 'Cache_Lite.php'
*
* Comparing to Cache_Lite constructor, there is another option :
* $options = array(
* (...) see Cache_Lite constructor
* 'defaultGroup' => default cache group for function caching (string)
* );
*
* @param array $options options
* @access public
*/
function Cache_Lite_Function($options = array(NULL))
{
if (isset($options['defaultGroup'])) {
$this->_defaultGroup = $options['defaultGroup'];
}
$this->Cache_Lite($options);
}

/**
* Calls a cacheable function or method (or not if there is already a cache for it)
*
* Arguments of this method are read with func_get_args. So it doesn't appear
* in the function definition. Synopsis :
* call('functionName', $arg1, $arg2, ...)
* (arg1, arg2... are arguments of 'functionName')
*
* @return mixed result of the function/method
* @access public
*/
function call()
{
$arguments = func_get_args();
$id = serialize($arguments); // Generate a cache id
if (!$this->_fileNameProtection) {
$id = md5($id);
// if fileNameProtection is set to false, then the id has to be hashed
// because it's a very bad file name in most cases
}
$data = $this->get($id, $this->_defaultGroup);
if ($data !== false) {
$array = unserialize($data);
$output = $array['output'];
$result = $array['result'];
} else {
ob_start();
ob_implicit_flush(false);
$target = array_shift($arguments);
$argu = $target;
if (strstr($target, '::')) { // classname::staticMethod
list($class, $method) = explode('::', $target);
$result = call_user_func_array(array($class, $method), $arguments);
} else if (strstr($target, '->')) { // object->method
// use a stupid name ($objet_123456789 because) of problems when the object
// name is the same as this var name
list($object_123456789, $method) = explode('->', $target);
global $$object_123456789;
$result = call_user_func_array(array($$object_123456789, $method), $arguments);
} else { // function
$result = call_user_func_array($target, $arguments);
}
$output = ob_get_contents();
ob_end_clean();
$array['output'] = $output;
$array['result'] = $result;
$this->save(serialize($array), $id, $this->_defaultGroup);
}
echo($output);
return $result;
}

}

?>

Fou-Lu
05-27-2011, 03:32 PM
If this script is function.php, then you cannot do what you are doing.
Call_user_func[_array] cannot be used to pass referenced values to a function (line 101). func_get_args are also non-referenced. You must explicitly call this frontpage() function or modify it to accept a non-referenced variable and return a result.


Depending what it does, you can promote to a 5.0+ class object, and use interfacing to achieve control on an Interfaced object type instead.

bngari
05-29-2011, 10:01 AM
The file is in a CMS website

Fou-Lu
05-29-2011, 05:34 PM
I don't care what its for, what I meant is what each of the class or objects are supposed to do.
What I'm saying is you cannot do this:


class Test
{
public static function dotest(&$var)
{
$var .= ', ' . $var;
}
}

$var = 'test';
call_user_func(array('Test', 'dotest'), $var);

As that will trigger a reference warning. call_user_func[_array] cannot be used to pass-by-reference parameters.

What needs to happen is either a common interface is implemented to every provided object so that it can force an invocation, or you can use reflection to generate this as well:



class Test
{
public $var;
public static function dostatictest(&$var)
{
$var = $var . ', ' . $var;
}
public function dotest(&$var)
{
$this->var = $var .= ', ' . $var;
}
}

$var = 'static ok';
$rm1 = new ReflectionMethod('Test', 'dostatictest');
$rm1->invokeArgs(null, array(&$var));
print $var . PHP_EOL;
$var = 'dynamic ok';
$obj = new Test();
$rm2 = new ReflectionMethod('Test', 'dotest');
$rm2->invokeArgs($obj, array(&$var));
print 'var: ' . $var . PHP_EOL;
print 'member: ' . $obj->var . PHP_EOL;


invokeArgs must be used; invoke will trigger a deprecation notice against the call-time pass-by-reference. I don't know if this is an oversight on the zend team, or if its a feature that will continue allowing us call-time pass-by-reference against the reflection's invoke.

Also note that this will work:


$var = 'call';
call_user_func_array(array('Test', 'dostatictest'), array(&$var));
print 'call: ' . $var . PHP_EOL;

without triggering a notice, but it is explicitly stated by the api to be deprecated (and makes note of the failure to trigger a notice): http://php.ca/manual/en/function.call-user-func-array.php#refsect1-function.call-user-func-array-notes. This is the reason why I cannot be sure that the invokeArgs is working as intended, even though there is no reference to an untriggered deprecation notice. This is why I would personally take an interface approach so I can be guaranteed of specific methods availability on an object. I don't believe this would work with static calls though, but I have never tried that.

bngari
06-02-2011, 08:53 AM
Thanks for the help but the code logic is okay. The problem was caused by an upgrade of the php version from 5.2 to 5.3. A few lines of extra code solved the issue. Cheers



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum