(php) 变量类
我有多个其他类所需的一组变量。我扩展了一个“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
听起来你想要这样的东西:
你也可以“假”继承多个类,如前面的 Stack Overflow 问题所示: 我可以在 PHP 中使用 1 个以上的类来扩展一个类吗?。
It sounds like you want something like this:
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?.