Why am I getting strict standards message?

I created a Zend Framework model by extending Zend_Db_Table_Absract as follows (simplified example):

 class Foos extends Zend_Db_Table_Abstract { protected $_schema = 'Foo'; protected $_name = 'Foos'; protected $_primary = 'id'; protected $_sequence = true; public function insert($data) { $db = $this->getAdapter(); $record = array('field1' => $data['field1'], 'field2' => $data['field2'], ... ); return parent::insert($record); } } 

The above correctly inserts the record. The problem is that I keep getting the following notification:

 Strict Standards: Declaration of Foos::insert() should be compatible with that of Zend_Db_Table_Abstract::insert() in /x/x/x/Foo.php on line XX 

As far as I can tell by reading the documentation and the API several times, the way I do this is the right one. I know that I can disable E_STRICT , but I would rather understand why I get the above notification. Any ideas? (PHP 5.3, Zend Framework 1.10)

+4
source share
4 answers

Mchl is mostly true, but the error you get is a parameter that doesn't exactly match ie:

 public function insert($data) { 

it should be:

 public function insert(array $data) { 

Pay attention to the specifier of type array to $data , the mixed view that you see is the return type, the type of the argument is array .

+12
source

it should be public function insert(array $data) (note the typehint array before $ data)

+2
source

The error itself is that you have E_STRICT in error_reporting or error_reporting ini directive ...

In principle, this suggests that it is not a good practice to change the "signature" of a function through inheritance. Example:

 class ParentClass { public function doFoo() {} } class ChildClass extends ParentClass { public function doFoo($bar) {} } 

This will create this error because the parent and child signatures do not match. Now the signature looks like the parser, so:

 public function doFoo($bar) {} public function doFoo($baz) {} 

Are both suitable signatures. Variable names do not have to be the same, but the number of variables, their order, their type tips and their default values โ€‹โ€‹must be the same.

 public function doFoo(array $bar, ParentClass $somethign, $biz = 'no') {} public function doFoo(array $baz, ParentClass $parent, $buz = 'no') {} 

They also match, but this is not the case:

 public function doFoo(array $baz, ParentClass $parent, $buz = 'no') {} public function doFoo(array $baz, ChildClass $parent, $buz = 'no') {} 

This is not necessarily โ€œbadโ€ (wireframes and developers do this all the time). Currently, the language supports this well. The reason for the E_STRICT error is that in the future the language may not support it. Therefore, it โ€œwarns youโ€ that it might be a bad idea to use your design like this ...

+1
source

Since the Foos :: insert () method has a different signature (argument list, access modifier) โ€‹โ€‹from Zend_Db_Table_Abstract :: insert (). According to strict rules, methods in the child class that override methods in the parent class must have the same signature.

[edit]

At least this is what usually means because, looking at the Zend_Db_Table_Abstract :: insert () documentation, I see nothing else ...

[edit]

Check out Zend_Db_Table_Abstract :: insert () if there is a type hint for the $ data argument. May be public function insert(array $data) {...

0
source

All Articles