Domanda

I've run into a problem and I'm not sure if this is just normal behaviour or if I wrote something wrong. I have a method in my base class that applies a global filter to a given class by way of creating a proxy for all new instances of that particular class. The way I planned to go about it is as follows:

  1. Attach static $global_filter (the proxy) to the class I want to be filtered, which extends the base class object
  2. Via my loading mechanism, return the proxy instead of the actual class upon new instantiations (which will mask method calls and apply filters accordingly)

However, I am getting stuck in step 1 and it seems that when I try to assign static $global_filter to the descendent class I want filtered, my base class object also gets the same assignment, which breaks everything else that extends from it.

Please see below for relevant code:

class object {

    public static $global_filter;

    public function _filterGlobal($class, $method, $callback) {
        if ( !is_object($class::$global_filter) ) {
            $class::$global_filter = new filterable(null);
            # Replace the object being called with the new proxy.
        }
        var_dump($class);
        var_dump($class::$global_filter); // `filterable`
        var_dump(\core\blueprint\object::$global_filter); // Returns same as line above
        die();
        return $class::$global_filter->_add($method, $callback);
    }

}

Both $class::$global_filter and \core\blueprint\object::$global_filter (the base class) are returning same instance. Whereas I expected object::$global_filter to be null.

I'm not using late static binding in order to preserve consistency (both single-object filters and global filters are called much in the same way non-statically).

This question seems relevant

Any help will be much appreciated :)

Edit, full example

This would be a concrete class, which extends model which extends object

<?php
use core\blueprint\model;
class modelMock extends model {
    protected $schema = array();
    public function method($test) {
        return $test;
    }
}

This would be another object (e.g a controller), which extends object aswell. It applies a filter to all new instances of model

<?php
use core\blueprint\object;
class objectMock extends object {

    public function applyFilters() {
        $this->_filterGlobal('core\blueprint\model', 'method', function($self, $chain) {
            $chain->params[0] = 'new param'; // adjust the paramters
            return $chain->next();
        });
    }

}
È stato utile?

Soluzione 3

Thanks everyone for you help, I spent some time on it this morning and managed to solve my problem. It's a bit of a workaround but here's how it goes:

public function _filterGlobal($class, $method, $callback) {
    if ( !is_object($class::$global_filter[$class]) ) {
        $class::$global_filter[$class] = new filterable(null);
        # Replace the object being called with the new proxy.
    }
    return $class::$global_filter[$class]->_add($method, $callback);
}

So basically in order to get unique static variables working in child classes without having to explicitly define them, you can use an array that stores the child's class name as a key and then access these variables via a getter.

Altri suggerimenti

when I try to assign static $global_filter to the descendent class I want filtered, my base class object also gets the same assignment

Yes, indeed this happens. A static property in essence is a global variable, constrained within the class's namespace. Running into problems with global variables is often an indication you're not using the best solution.

To solve your problem, you could make the filter a (non-static) property:

$class->$filter = new Whatever();

But as always, there's more roads that lead to Rome, and I would advise you to look for alterative ways to do it.

I don't know if this is a help for you:

class a {
   public static $type;

   public static function setType($class, $newType) {
      $class::$type = $newType;
      var_dump($class::$type);
   }
}
class b {
   public static $type = 'myType';
}

var_dump(b::$type);
a::setType('b', 'yourType');
var_dump(a::$type);

May be you have not defined the static property to the concrete class.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top