(php) 变量类

发布于 2024-11-16 15:40:24 字数 2577 浏览 0 评论 0原文

我有多个其他类所需的一组变量。我扩展了一个“nice”的 getter 函数(猜测 var 名称并动态生成“set”/“get”函数)来与 setter 一起使用。

示例:

在接口/父级/其他中: public $name;

在加载 'mySetterGetter' 类的其他类中: $set_get = new mySetterGetter(); $set_get->get_name();

遗憾的是,我无法在接口中使用变量,也无法使用多个父类扩展一个类。是否有其他方法来加载这些“接口”/扩展 Set/Get 类?

我需要做的是以下内容:

// 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 <[email protected]> 
 * 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;
}
}

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 <[email protected]> 
 * 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;
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

擦肩而过的背影 2024-11-23 15:40:24

听起来你想要这样的东西:

// 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;
}

你也可以“假”继承多个类,如前面的 Stack Overflow 问题所示: 我可以在 PHP 中使用 1 个以上的类来扩展一个类吗?

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?.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文