0

Please, could you help me a bit with my problem?

I have class called Translateable and then clasess Article and Banner, which extend this class.

Problem occurs, when I do this:

$article = (new Article)->find(15);
$banner =  (new Banner)->find(1);

$articleTrans = $article->trans(); // method trans is method from Translateable

When I call $article->trans(); I expect output like this:

App\Models\ArticleTrans
Article

but it return this:

App\Models\ArticleTrans
Banner

First row is ok, but the second one if bad and I don't know, how to solve this problem. I need to have $instance stored as static property.

Could you give me you help?

  class Translateable extends Model {

        static $transLang = null;
        static $transClass = null;
        static $instance = null;

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

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

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

        }
        /**
         * get items trans
         *
         * @param null $lang
         * @return mixed
         */
         public function trans($lang = null) {
              if($lang == null) {
                 $lang = static::$transLang;
              }

                  echo static::$transClass;
                  echo class_basename(static::$instance);
                  die();

              }
              public static function find($primaryKeyVal, $columns = []) {

                 $tci = new static::$transClass;
                 $item = static::withTrans()->where(static::$instance->getTable() . '.' . static::$instance->primaryKey, '=', $primaryKeyVal)->where($tci->getTable() . '.lang', '=', static::$transLang)->first();
    return $item;

        }
    }

class Article extends Translateable {
        static $transClass = 'App\Models\ArticleTrans';

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

            parent::$transClass = static::$transClass;
            parent::$instance = $this;

            parent::__construct($attributes);

        }
 }

class Banner extends Translateable {

    static $transClass = 'App\Models\BannerTrans';

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

        parent::$transClass = static::$transClass;
        parent::$instance = $this;

        parent::__construct($attributes);

    }
}
Jan Kožušník
  • 683
  • 3
  • 16
  • 30
  • 1
    Sounds like you're using `statics` without understanding what they are. It looks like you should be using class properties. http://php.net/manual/en/language.oop5.static.php for more info on static. – Jonnix May 29 '16 at 21:08
  • Oh, and could you provide me code example please? – Jan Kožušník May 29 '16 at 21:09
  • Nope but the docs for class properties can be found at http://php.net/manual/en/language.oop5.properties.php – Jonnix May 29 '16 at 21:10
  • I edit my code to version in my question, what is bad now? – Jan Kožušník May 29 '16 at 21:16
  • All you did was remove `public`... they're still all static. – Jonnix May 29 '16 at 21:20
  • Yeah, I thought, it is the fail, you mean. So what should I do? When I remove "static" I cannot access them in static method. I'm quite confused. – Jan Kožušník May 29 '16 at 21:22
  • There are no static methods in the code you've shown us. Do you know why you're using `static`? – Jonnix May 29 '16 at 21:25
  • I need it to be static, because I access it in another method, not just in methods in code I post... I add method, where I access it too.. and this method need to be static... because it's method from Laravel framework and I overwrite it... – Jan Kožušník May 29 '16 at 21:27
  • Ah. Don't know much about Laravel so can't really comment. All I can say is as your code is, it's doing exactly what you're telling it to. Perhaps somebody else has a better idea of how to get this to work with these constraints. – Jonnix May 29 '16 at 21:33
  • No problem, I know, I have failure in my code, but I don't know, where and how to solve it. Anyway, don't you have any idea, how to prevent revriwing **$instance** property content? – Jan Kožušník May 29 '16 at 21:37
  • @JanKožušník method from which part of the Laravel framework? I assume the `Model` base class is self written because I don't see a Laravel namespace. Therefor I'm convinced Laravel doesn't use any static properties by default to be used somewhere as an accessor for class assets. Could you post only the relevant parts where you say _because it's a method from the Laravel framework and I overwrite it_? – dbf May 30 '16 at 08:33
  • I didn't post namespace, my model is extending laravel5 default model. I'm not sure how really laravel5 make it, anyway, you're right, because e.g. `$primaryKey` or `$table` are non static properties. But then, how do they access it in methods, called like this: `Model::find()` ? It's static method in which you cannot access non-static properties. – Jan Kožušník Jun 01 '16 at 13:09

0 Answers0