2016-03-27 35 views
-2

私が初めて直面している非常に奇妙な問題を解決しています。 プロパティstatic::$somePropertyを持つ主要なクラスがいくつかあります。このクラスを他のクラスに拡張します。 ClassAおよびClassB。私はPHPオブジェクトのプロパティが上書きされました

$classA = ClassA 

をロードし、そこ

static::$someProperty = "ClassA" 

を設定し、この値をエコーするとき 問題は、今、それが正常に動作して"ClassA"を返すが、その後、私はまた

$classB = ClassB 

とセットをロード

static::$someProperty = "ClassB" 

とするとき、私$classA

echo static::$someProperty 

今、値"ClassB"があります。

この問題を解決するにはどうすればよいですか?おそらくそれはstaticと接続されていますが、私はこれで何をするべきではありません。

class Translateable extends Model{ 

     public static $transLang; 
     public static $transClassInstance; 
     public static $instance; 

     public $transInstance = null; 

     public function __construct(array $attributes = array()) { 
      self::$transLang = App::getLocale(); 

      $tcName = static::$instance->transClass; 
      static::$transClassInstance = new $tcName; 
      parent::__construct($attributes); 
     } 

    /** 
    * add trans to the item 
    * 
    * @return mixed 
    */ 
    public static function withTrans($lang = null) { 

     if($lang == null) { 
      $lang = static::$transLang; 
     } 

     return static::join(static::$transClassInstance->getTable(), function ($join) use ($lang) { 
      $join->on(static::$instance->getTable() . '.' . static::$instance->primaryKey, '=', static::$transClassInstance->getTable() . '.' . static::$instance->primaryKey)->where(static::$transClassInstance->getTable() . '.lang', '=', $lang); 
     })->where(static::$transClassInstance->getTable() . '.lang', '=', $lang) 
      ; 

    } 
    } 




    class Nested extends Translateable{ 

     // protected $lft, $lvl, $rgt, $parent_ID; 

     public static $transClassInstance; 
     public static $transLang; 


     public function __construct(array $attributes = array()) { 

      self::$transLang = App::getLocale(); 

      $tcName = static::$instance->transClass; 
      static::$transClassInstance = new $tcName; 

      parent::$instance = $this; 
      parent::__construct($attributes); 

     } 
    /** 
    * 
    * get $this item child 
    * 
    * @return null 
    */ 
    public function getChilds() { 

     $primaryKeyName = $this->primaryKey; 
     $parent_id = $this->$primaryKeyName; 
     // here is echo PageTrans instead of ProductCategoryTrans 
     echo static::$transClassInstance->getTable().'<br/>'; 
     echo static::$transClassInstance->getTable() . '.lang'.'<br/>'; 
     $query = static::where('parent_ID', '=', $parent_id)->where(static::$transClassInstance->getTable() . '.lang', '=', static::$transLang); 
     echo $query->toSql(); 
     $this->generateItemsQuery($query); 
     $query->orderBy('lft', 'ASC'); 
     $categories = $query->get(); 

     return $categories; 

    } 


    } 


class ProductCategory extends Nested{ 
    public $transClass = 'App\Models\ProductCategoryTrans'; 

    public function __construct(array $attributes = array()) { 

     static::$instance = $this; 
     parent::__construct($attributes); 

    } 
} 

class Page extends Nested{ 
    public $transClass = 'App\Models\PageTrans'; 

    public function __construct(array $attributes = array()) { 

     static::$instance = $this; 
     parent::__construct($attributes); 

    } 
} 

使用例:

// find product category with ID == 1 
$productCategory = (new ProductCategory)->find(1); // "ClassA" 

// get some page... 
$page = (new Page)->find(1); // find page with ID == 1 // "ClassB" 

// get childs of loaded category 
$categoryChilds = $productCategory->getChilds(); // get this category 
+0

selfを使用してみてください私が試し – RomanPerekhrest

+1

(メインクラスを拡張し、それぞれの子クラス内の静的プロパティを設定する)いくつかのコードを示しインラインコードからの書式設定を改善します。コードブロックとしてフォーマットするには 'ctl-k'を使います。これを完全なPHP文に拡張することをお勧めします。 –

+0

'$ classA = ClassA'の意味を説明できますか? 'ClassA'は' ClassA'クラスのオブジェクトですか? – SOFe

答えて

0

は、クラスAとClassBの

self::$someProperty = 'test'; 
+0

これを使用すると、別の問題を解決する必要があります - > $ transClassInstance **変数ストアインスタンスオブジェクト(新しいProductCategory) –

関連する問題