开发者

(php) variable class

开发者 https://www.devze.com 2023-03-14 16:57 出处:网络
I have a set of variables that i need for multiple other classes. I extended a \'nice\' getter function (that guesses for the var name and produces \'set\'/\'get\' functions on the fly) to work with s

I have a set of variables that i need for multiple other classes. I extended a 'nice' getter function (that guesses for the var name and produces 'set'/'get' functions on the fly) to work with setters to.

Example:

In interface/parent/whatever: public $name;

In some other class that loads the 'mySetterGetter' class: $set_get = new mySetterGetter(); $set_get->get_name();.

Sadly I can't use variables in an interface and can't extend a class with multiple parent classes. Is there some other way to load these "interfaces"/extend the Set/Get class?

What I need to do is the following:

// This should be my "interface" one
class myIntegerInterface
{
public $one;
public $two;
public $three;
}
// This should be my "interface" two
class myStringInterface
{
public $foo;
public $bar;
public $whatever;
}

// This is my setter/getter class/function, that needs to extend/implement different variable classes
class mySetterGetter implements myIntegerInterface, myStringInterface
{
/**
 * Magic getter/setter method
 * Guesses for a class variable & calls/fills it or throws an exception.
 * Note: Already defined methods override this method.
 * 
 * Original @author Miles Keaton <mileskeaton@gmail.com> 
 * on {@link http://www.php.net/manual/de/language.oop5.overloading.php#48440}
 * The function was extended to also allow 'set' tasks/calls.
 * 
 * @param (string) $val | Name of the property
 * @param unknown_type $x | arguments the function can take
 */
function __call( $val, $x )
{
    $_get = false;

    // See if we're calling a getter method & try to guess the variable requested
    if( substr( $val, 0, 4 ) == 'get_' )
    {
        $_get = true;
        $varname = substr( $val, 4 );
    }
    elseif( substr( $val, 0, 3 ) == 'get' )
    {
        $_get = true;
        $varname = substr( $val, 3 );
    }

    // See if we're calling a setter method & try to guess the variable requested
    if( substr( $val, 0, 4 ) == 'set_' )
    {
        $varname = substr( $val, 4 );
    }
    elseif( substr( $val, 0, 3 ) == 'set' )
    {
        $varname = substr( $val, 3 );
    }

    if ( ! isset( $varname ) )
        return new Exception( "The method {$val} doesn't exist" );

    // Now see if that variable exists:
    foreach( $this as $class_var => $class_var_value )
    {
 开发者_运维百科       if ( strtolower( $class_var ) == strtolower( $varname ) )
        {
            // GET
            if ( $_get )
            {
                return $this->class_var_value;
            }
            // SET
            else 
            {
                return $this->class_var_value = $x;
            }
        }
    }

    return false;
}
}


It sounds like you want something like this:

// An abstract class that can't be instantiated but which provides a __call method to other classes that extend this one.
abstract class mySetterGetter
{
  function __call($val, $x)
  {
    $_get = false;

    // See if we're calling a getter method & try to guess the variable requested
    if( substr($val, 0, 4) == 'get_' )
    {
      $_get = true;
      $varname = substr($val, 4);
    }
    elseif( substr($val, 0, 3) == 'get' )
    {
      $_get = true;
      $varname = substr($val, 3);
    }

    // See if we're calling a setter method & try to guess the variable requested
    if( substr($val, 0, 4) == 'set_' )
      $varname = substr($val, 4);
    elseif( substr($val, 0, 3) == 'set' )
      $varname = substr($val, 3);

    if ( ! isset($varname) )
      throw new Exception("The method {$val} doesn't exist");

    // Now see if that variable exists:
    foreach( $this as $class_var => $class_var_value )
    {
      if ( strtolower($class_var) == strtolower($varname) )
      {
        // GET
        if ( $_get )
          return $this->class_var_value;
        // SET
        else
        {
          $this->class_var_value = $x[0];

          return;
        }
      }
    }

    return false;
  }
}

// myString
class myString extends mySetterGetter
{
  public $foo;
  public $bar;
  public $whatever;
}

// myInteger
class myInteger extends mySetterGetter
{
  public $one;
  public $two;
  public $three;
}

You can also "fake" inheriting by multiple classes as in this previous Stack Overflow question: Can I extend a class using more than 1 class in PHP?.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号