Welcome Guest, Not a member yet? Register   Sign In
Entity magic __get not called in second generation child class
#1

I am working on an application where I have two very closely related types of data, sort of on par with Shape and ColoredShape.  ColoredShape implements all the features and has all the types of shape, but has new, cool features that regular old Shape wants nothing to do with.  That is a bit simplistic, but consider they might be implemented as follows:
PHP Code:
class Shape implements \CodeIgniter\Entity\Entity
{

}

class 
ColoredShape extends \App\Entities\Shape
{
  public function getColor()
  {
    // this throws a property not found exception
    // ignore how useless this is in production.
    return $this->color;
  }



Within the ColoredShape class, I have the getColor() method.  When I try to access the $this->color property, I get a property not found exception.  I did some digging and found that the ColoredShape class doesn't use the __get() method defined in the base Entity class.  I also found that if I add a __get() magic method in Shape, ColoredShape will access Shape's __get() method.  So, I'm wondering if my understanding of inheritance is wrong or incorrect.  Is it possible to use the Entity class's __get() method a few generations down?
Another interesting thing I found is that if you use parent::__get() within the ColoredShape class, an out of memory exception is generated.  Just with a quick glance, I couldn't see the reason for that to happen.  Can anyone explain what might cause that?
Finally, would a trait be useful here?  I.e., I would move all the "shape" logic into a shape trait, then implement the unique colored shape within the ColoredShape class, and inheriting directly from the base Entity class.
Reply
#2

You cannot get the property value in your method. Looping occurs as getColor() => $this->color => getColor() ... and property not exist
Try rename method or replace return $this->attributes["color"]
Simple CI 4 project for beginners codeigniter-expenses ( topic )
Reply
#3

Thanks. I think part of my goal was to continue to use the __get() logic for casting. But this particular column not something that can or should be casted.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB