1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
<?php class MyClass{ function __construct(){ // do something } function MyClass() { $args = func_get_args(); call_user_func_array(array(&$this, ‘__construct’), $args); } } //or class MyClass{ function __construct(){ $this->MyClass() } function MyClass() { // do something } } ?>
Refactorings
No refactoring yet !
Fabrice Luraine
November 9, 2007, November 09, 2007 18:13, permalink
I think the first one is preferable: it's better to write for php5 and keep a backward compatibility for php4
Hubert Roksor
November 11, 2007, November 11, 2007 02:41, permalink
Actually, I would use neither because defining two potential constructors generates a E_STRICT notice in PHP5. I would only define one constructor, PHP4 style.
Note that I would strongly advise pondering the development of a PHP4 application considering its imminent death. OOP is a real pain in PHP4 and you wouldn't exploit any of PHP5's strengths.
jim
January 28, 2008, January 28, 2008 15:56, permalink
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
<?php class MyClass{ function __construct(){ // do something } function MyClass() { $args = func_get_args(); call_user_func_array(array(&$this, ‘__construct’), $args); } } //or class MyClass{ function __construct(){ $this->MyClass() } function MyClass() { // do something } } ?>
Which one is the best cross version php 4/5 constructor?