2014-01-10 38 views
0

我有一个样品类这样有效的方式

class foo{ 
    private $a = null; 
    private $b = null; 
    private $c = null; 
    private $d = null; 

function a(){$a = ''; return $this; } 
function b(){$b = '0'; return $this; } 
function c(){$c = 'x'; return $this; } 
function d(){$d = 'false'; return $this; } 

function z(){ 
    //I would like to only echo properties that are not null. 
    //and I am looking for better alternative than using if/else approach. 
    } 
} 

这是我想调用的方法。

$o = new foo(); 
$o->a() 
    ->b() 
    ->c() 
    ->d() 
    -z(); 

因此,现在方法z()应该回显非空的属性。由于我有很多属性,使用if/else将被证明是困难/冗长的。

+1

一种方法是将您的属性转换为该类内的本地数组。好处是可以使用array_filter(),它将返回一个数组减去原始数组中的空键。 http://us2.php.net/manual/en/function.array-filter.php – Rottingham

+0

@kuroineko http:// stackoverflow。com/questions/21050862 /高效的方法来检查堆栈的非空属性#comment31651251_21050976 – user2679413

回答

0

在你的类,你可以使用get_object_vars()获得当前范围内可用的属性:

function z() 
{ 
    foreach (get_object_vars($this) as $key => $value) 
    { 
    if (is_null($value)) 
    { 

    } 
    else 
    { 

    } 
    } 
} 
+0

这是一个非常好的方法。谢谢。但是,正如我试过的那样,它考虑到了在构造函数中做出的任何赋值属性。换句话说,在这种情况下,'$ this-> a'作为一个属性需要'function __construct($ foo){$ this-> a = $ foo;}'。 – user2679413

+0

@ user2679413我不确定你想要什么,如果你需要检查默认值,你可以使用'get_class_vars(foo)'代替。 – jeroen

0

您可以通过$this迭代,这将给你通过foreach ($this as $key=>$value)属性名称和值 - 在那里你可以轻松使用empty()

请参阅this working example

public function echoAllProperties() { 
    foreach ($this as $key=>$value) { 
     if (!empty($value)) 
      echo $key." => ".$value."\n"; 
    } 
} 
0
$o = new foo(); 

    $class_vars = get_class_vars(get_class($o)); 

    foreach ($class_vars as $name => $value) { 
     echo "$name : $value\n"; 
    } 
0

编辑:

看来你使用的是自己的PHP对象的属性是什么定义,所以让我给你我的。

PHP文档对于主题有些模糊,有些功能如get_object_vars有点误导,因为它们将获取对象属性而不是变量。

据我的理解,是什么它归结为是:PHP使得性能之间的区别(在编译时已知),并动态特性(即设置没有那些已被宣布)。

它与设置属性有关(因为它会创建一个动态的属性),但重点是要知道属性是否在类定义内部声明。

如果实际上你感兴趣的类和对象属性之间的这种差异,你可能会发现API的快乐,尽管如果你问我,我发现它使用起来极其尴尬。

此API的主要优点是,您可以列出将从当前范围(通常为受保护或私有成员)无法访问的属性。
如果您只是使用get_object_varsget_class_vars函数,则会忽略所有私有/受保护属性,除非您在对象本身内定义一个方法(继承调用get_class_vars的类将允许您获取受保护的属性,但不能访问私有那些)。

function list_properties($obj, $filter = null) 
{ 
    if ($filter === null) $filter = ReflectionProperty::IS_STATIC 
            | ReflectionProperty::IS_PUBLIC 
            | ReflectionProperty::IS_PROTECTED 
            | ReflectionProperty::IS_PRIVATE; 

    $spy = new ReflectionClass ($obj); 
    $class_properties = $spy->getProperties($filter); 

    echo "class:<br />"; 
    foreach ($class_properties as $prop) 
    { 
     $name = $prop->getName(); 
     $p = $spy->getProperty ($name); 
     $p->setAccessible(true); 
     $value = $p->getValue($obj); 
     echo "$name ("; var_dump($value); echo ")<br />"; 
    } 
    echo "<br />object:<br />"; 
    foreach ($obj as $property => $value) 
     { echo "$property ("; var_dump($value); echo ")<br />"; } 
} 

class foo { 
    protected $class_protected; 
    private $class_private; 
    public $class_public; 
    private static $static_protected = "azerty"; 
    private static $static_private = "qsdfgh"; 
    public static $static_public = "wxcvbn"; 
} 


$v = new foo(); 
$v->dynamic = "xxx"; 
list_properties($v); 

// the ReflectionClass filter flags semantic is broken, 
// so filtering becomes soon a cruel pain in the *ss 
/* 
* I leave it to the reader as an execrise to coax the bloody thing 
* into giving you only the static public class properties, for instance. 
*/ 
list_properties($v, ReflectionProperty::IS_STATIC|ReflectionProperty::IS_PUBLIC); 
+0

问题是在构造函数中初始化了vars。 – user2679413

+0

???它需要属性和当前值。除了$ v,$ property和$ value之外,此代码中没有变量。 –

+0

我在说,无论在构造函数里面定义的是什么属性,我都不想在函数中使用 – user2679413