Created
March 18, 2011 13:48
-
-
Save commana/876090 to your computer and use it in GitHub Desktop.
Which constructor takes precedence in PHP?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
PHP allows two possibilities to define a constructor: | |
1) Using the classic, Java-like way with a method named like the class, or | |
2) with the magic method named __construct. | |
Of these two possibilities, which one takes precedence? | |
*/ | |
class ConstructorCheck { | |
public function __construct() { | |
echo "__construct() called", PHP_EOL; | |
} | |
// Strict standards: Redefining already defined constructor | |
public function ConstructorCheck() { | |
echo "ConstructorCheck() called", PHP_EOL; | |
} | |
} | |
class ConstructorCheckTwo { | |
public function ConstructorCheckTwo() { | |
echo "ConstructorCheckTwo() called", PHP_EOL; | |
} | |
// Strict standards: Redefining already defined constructor | |
public function __construct() { | |
echo "__construct() called", PHP_EOL; | |
} | |
} | |
$c1 = new ConstructorCheck(); // __construct() called | |
$c2 = new ConstructorCheckTwo(); // __construct() called |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment