class php constructor code example
Example 1: php contruct parent
<?php
class BaseClass {
function __construct() {
print "In BaseClass constructor\n";
}
}
class SubClass extends BaseClass {
function __construct() {
parent::__construct();
print "In SubClass constructor\n";
}
}
class OtherSubClass extends BaseClass {
}
$obj = new BaseClass();
$obj = new SubClass();
$obj = new OtherSubClass();
?>
Example 2: php 8 constructor
class Point {
public function __construct(
public float $x = 0.0,
public float $y = 0.0,
public float $z = 0.0,
) {}
}
Example 3: php constructor
__construct ([ mixed $args = "" [, $... ]] ) : void