Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7

Thread: PHP array error

  1. #1
    New Coder
    Join Date
    Nov 2010
    Location
    Kenya
    Posts
    42
    Thanks
    4
    Thanked 1 Time in 1 Post

    PHP array error

    Hi, a website i'm working on is having a problem. It is giving an error like
    PHP Code:
    WarningParameter 2 to frontpage() expected to be a referencevalue 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.

  • #2
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    822
    Thanks
    93
    Thanked 18 Times in 18 Posts
    What the code causing the error? Line 101 on Functions.php

  • #3
    New Coder
    Join Date
    Nov 2010
    Location
    Kenya
    Posts
    42
    Thanks
    4
    Thanked 1 Time in 1 Post
    PHP Code:
    <?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;
        }

    }

    ?>

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    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.

    Edit:
    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.
    Last edited by Fou-Lu; 05-27-2011 at 02:35 PM.

  • #5
    New Coder
    Join Date
    Nov 2010
    Location
    Kenya
    Posts
    42
    Thanks
    4
    Thanked 1 Time in 1 Post
    The file is in a CMS website

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    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:
    PHP Code:
    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:
    PHP Code:

    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:
    PHP Code:
    $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.cal...nc-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.

  • #7
    New Coder
    Join Date
    Nov 2010
    Location
    Kenya
    Posts
    42
    Thanks
    4
    Thanked 1 Time in 1 Post
    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


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •