-2

I'm solving quite strange problem which I'm facing for the first time. I have some main Class with property static::$someProperty and I extend with this class other classes, e.g. ClassA and ClassB. Problem is now, when I load

$classA = ClassA

and set there

static::$someProperty = "ClassA"

and echo this value, it works fine and return "ClassA" but then I also load

$classB = ClassB

and set

static::$someProperty = "ClassB"

and when I

echo static::$someProperty

in $classA now, there is value "ClassB".

Do you know, how to solve this problem? Probably it is connected with static, but I don't now, what to do with this.

    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);

    }
}

Example usage:

// 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
Jan Kožušník
  • 683
  • 3
  • 16
  • 30

1 Answers1

0

Try to use self in classA and classB

self::$someProperty = 'test';
Veniamin
  • 459
  • 2
  • 9
  • When I use this, I need to solve another one problem -> it in **$transClassInstance** variable store instance for first object (new ProductCategory) – Jan Kožušník Mar 27 '16 at 17:49