PHP isset ($ this) and using the same object method in a static and object context

I am working on a class that should be accessible through calls to static functions, as well as object methods. I found that I duplicate the logic for multiple functions.

A simplified example:

class Configurable{

    protected $configurations = array();

    protected static $static_configurations = array();

    public function configure($name, $value){

        // ...lots of validation logic...

        $this->configurations[$name] = $value;

        }

     public static function static_configure($name, $value){

        // ...lots of validation logic (repeated)...

        self::$static_configurations[$name] = $value;

        }

    }

I found a solution for this, but it feels really dirty:

class Configurable{

    protected $configurations = array();

    protected static $static_configurations = array();

    public function configure($name, $value){

        // ...lots of validation logic...

        if (isset($this)){
            $this->configurations[$name] = $value;
            }
        else{
            self::$static_configurations[$name] = $value;
            }

        }

    }

I also need a static function so that I can set configurations throughout the application. Also, the good thing with this method is that I can use the same method names in both areas.

Are there any problems with the testing area? Performance issues, compatibility issues, etc. All this works for me in PHP 5.2, and I don't need to support <5.

+5
4

, , E_STRICT. :

: Foo:: bar() /home/yacoby/dev/php/test.php 10

PHP6 , E_STRICT E_ALL. , E_ALL , .

. , .

+2

, - . , .

, , , . - . :

class DynamicClass {
    protected $foo;
    protected $bar;
    public function baz($arg1) {
        return StaticClass::bar($this->foo, $arg1);
    }
    public function zop($arg1, $arg2) {
        return StaticClass::zop($this->foo, $this->bar, $arg1, $arg2);
    }
    // Context-less helper function
    public function womp($arg1) {
        return StaticClass::womp($arg1);
    }
}

class StaticClass {
    public static function baz(&$fooContext, $arg1) { ... }
    public static function zop(&$fooContext, &$barContext, $arg1, $arg2) { ... }
    public static function womp($arg1) { ... }
}

, - , . , , ( , , , , , ), . ( DynamicClass , DynamicClass , .


, Singleton. , , Configurable, Configurable s. singleton , , ( $_GLOBALS ..). :

class DynamicClass {
    protected $foo;
    protected $bar;

    public function baz($arg1) { ... }
    public function zop($arg1, $arg2) { ... }

    public static function getSingleton() {
        static $instance = null;
        if ($instance === null) $instance = new DynamicClass();
        return $instance;
    }
}

, , DynamicClass::getSingleton(). , . , , .

+2

. :

   TestRecord::generateForm(); // Generate an empty form.

   $test = new TestRecord( $primaryKey );
   [...]
   $test->generateForm();      // Generate an edit form with actual $test values.

/ , , .

PHP 5.3 , __call, __callStatic static:::

public function __call( $name, $args )
{
  if ( $name == 'generateForm' ) {
    $this->fields = static::createFields();  // Action 1 : static.
    $this->fillFields();                     // Action 2 : instance.
    static::renderForm( $this->fields );     // Action 3 : static.
  }
}

public static function __callStatic( $name, $args )
{
  if ( $name == 'generateForm' ) {
    $fields = static::createFields();        // Action 1 : static.
                                             // Action 2 : none.
    static::renderForm( $fields );           // Action 3 : static.
  }
}

. static:: , 3 (createFields, fillFields rendreForm) protected , abstract, , PHP : , . , OO.

+2

core php, index.php?var=, oop php, .

-1
source

All Articles