-2

Trying to replace a deprecated line with:

if (new My_Model())->_caching($class)

This broke the PHP7 app with the operator " -> "

PHP Parse error: syntax error, unexpected '->' (T_OBJECT_OPERATOR)

I used above line when trying to replace:

if (self::_caching($class))

which was incorrectly produces a non-static method.

static function search($class,
                       $table,
                       $search_options,
                       $sql_options = array(),
                       $limit = -1,
                       $offset = 0)
{
    $ci = &get_instance();      
    $tenant_id = $ci->tenant->id;

    if (self::_caching($class))
    {
        // Check cache first
        $cached_objects_key = $class.'-'.md5(serialize($search_options).
                                serialize($sql_options).$limit.$offset);
        if ($cached_keys = $ci->cache->get($cached_objects_key, $class, $tenant_id))
        {
            $cached_objects = array();
            foreach ($cached_keys as $object_cache_key)
            {
                if ($cached_object = $ci->cache->get($object_cache_key, $class, $tenant_id))
                {
                    array_push($cached_objects, $cached_object);
                }
                else 
                {
                    // we can't complete the list, so break out
                    // and let the function continue
                    unset($cached_objects);
                    break;
                }
            }

This question was posed for discussing operator -> error and how to remove. Not immediately having to do with referencing arrays.

PatrickLightning
  • 117
  • 1
  • 12
  • 1
    Read about the [`if` control structure](http://php.net/manual/en/control-structures.if.php). The condition must stay between parentheses. – axiac Aug 18 '17 at 21:03
  • 1
    you have a closing bracket in the wrong place, it should read `if (new My_Model()->_caching($class))`... but why are you changing the code? the static method `_caching()` should still work in php7! I think you should go back a step and if you had a problem then ask the question that motivated you to make this change – Wee Zel Aug 18 '17 at 21:06
  • 1
    `if ((new My_Model())->_caching($class))` – u_mulder Aug 18 '17 at 21:08
  • Wee Zel - The app breaks in PHP7 with the self method above. It's deprecated for v7. If you have a better idea how it can be replaced, would love to see it. Thanks. – PatrickLightning Aug 18 '17 at 21:12
  • did it say something like "Deprecated: Non-static method My_Model::_caching() should not be called statically"?? – Wee Zel Aug 18 '17 at 21:17
  • yes Wee Zel that's right – PatrickLightning Aug 18 '17 at 21:19
  • u_mulder's line seemed to work best . . . – PatrickLightning Aug 18 '17 at 21:23
  • if the declaration for the `_caching()` function is declared as `private function` (I expect it is, old style coding used the underscore to represent private functions) then change it to `private static function`. then revert other changes you have made and everything should be fine – Wee Zel Aug 18 '17 at 21:23
  • Miken32 please refer to last line in OP. – PatrickLightning Aug 18 '17 at 21:33

1 Answers1

1

Try changing if (new My_Model())->_caching($class)

Into: if ((new My_Model())->_caching($class))

You tried to acces the function on the if statement instead of the class.

Peter M
  • 1,059
  • 8
  • 19