Skip to content

Instantly share code, notes, and snippets.

@commana
Created March 18, 2011 13:48
Show Gist options
  • Save commana/876090 to your computer and use it in GitHub Desktop.
Save commana/876090 to your computer and use it in GitHub Desktop.
Which constructor takes precedence in PHP?
<?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