Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Under what circumstances is it possible that parent fields are not inherited?

I've got a curious bug (bugging me) today. There are three inheritance levels involved:

Grandpa:

abstract class Zend_Db_Table_Row_Abstract implements ArrayAccess, 
                                                     IteratorAggregate
{
    protected $_data = array();

    /* snip */
}

Mom:

namespace Survey\Db\Table\Row;

class AbstractRow extends \Zend_Db_Table_Row_Abstract
{
    /* snip */
}

Child:

namespace Survey\Db\Table\Row;

class SurveyItem extends AbstractRow implements ISkippable
{
    /* snip */
}

Exception:

Type:     ErrorException  
Value:    Undefined property: Survey\Db\Table\Row\SurveyItem::$_data  
Location: [...]/Zend/Db/Table/Row/Abstract.php in handleError , line 177  

Line 177 doesn't seem to be relevant, but I'm adding it just so you'd believe me ;)

if (!array_key_exists($columnName, $this->_data)) {

PHP 5.4.11, problem did NOT exist with PHP 5.4.8

When I saw the fix for Bug #63462 Magic methods called twice for unset protected properties, I thought, that wold solve the problem, since this bug leads to exactly the weird unexpected outcome I was seeing.

But it turns out, the problem still exists after updating to PHP 5.4.12. The likelyhood that there is another similar bug in PHP seems quite high.

Question:

I get the info that a protected field defined in the Grandpa is undefined in the Child. What scenarios can lead to such an outcome?

like image 248
markus Avatar asked Feb 21 '13 21:02

markus


1 Answers

following snippet works flawlessly on PHP 5.4.9:

class A
{
    protected $foo = 'hello';
    public function bar()
    {
        echo $this->foo;
    }
}

class B extends A {}

class C extends B {}

$c = new C();
$c->bar();

Please minimise your code step by step to this to see if/when problem occurs (I wonder why you haven't done it already)

If you are sure this worked on PHP 5.4.8 and does not work on PHP 5.4.11 then you found a bug in PHP and should be reporting it on php.net

Answer may be different (maybe it simply got 'unset' along the way). Minimise your code and you will know.

like image 96
fsw Avatar answered Nov 08 '22 10:11

fsw