1

プロパティが存在し、これが機能するかどうかを確認する必要があります。

class someClass {
  protected $some_var

  public static function checkProperty($property) {
    if(!property_exists(get_class()) ) {
      return true;
    } else return false;
  }
}

しかし、クラスを拡張しようとすると、もう機能しません。

class someChild extends someClass {
  protected $child_property;

}


someChild::checkProperty('child_property'); // false

必要な機能を取得するにはどうすればよいですか? 、、に置き換えget_class()てみましたが、何も機能しません。$thisselfstatic

4

3 に答える 3

0

私は正しい答えを見つけたと思います。静的メソッドの場合は、を使用しますget_called_class()

おそらく$thisオブジェクトメソッドで機能します。

于 2012-08-19T03:15:07.623 に答える
0

get_class() および get_parent_class() に対して property_exists をチェックするのはどうですか? ただし、より多くのネストされたクラスについては、クラスを再帰的にチェックする必要があります。

public static function checkProperty($property)
{
    if (property_exists(get_class(), $property)
        or property_exists(get_parent_class(), $property))
    {
        return true;
    }
    else return false;
}

(申し訳ありませんが、私はもっとオールマン スタイルに興味があります ;-))

于 2012-08-19T04:19:39.530 に答える
-1

以下の作品:

<?php

class Car
{
    protected $_var;

    public function checkProperty($propertyName)
    {
        if (!property_exists($this, $propertyName)) {
            return false;
        }
        return true;
    }
}

class BMW extends Car
{
    protected $_prop;
}

$bmw = new BMW();
var_dump($bmw->checkProperty('_prop'));

@param $class テストするクラス名またはクラスのオブジェクト

于 2012-08-19T03:04:43.560 に答える