-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathPHP Constructor
38 lines (38 loc) · 951 Bytes
/
PHP Constructor
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
30
31
32
33
34
35
36
37
38
<!DOCTYPE html>
<html>
<head>
<title>Constructor Example in PHP</title>
</head>
<body>
<?php
// PHP Constructor Example Code - CodesCracker
class MyBaseClass
{
function __construct()
{
echo "I am in MyBaseClass constructor<br/>";
}
}
class AnotherClass extends MyBaseClass
{
function __construct()
{
// this will call the parent constructor, which is in MyBaseClass class
parent::__construct();
// after the above, below will be printed
echo "I am in AnotherClass constructor<br/>";
}
}
class AnotherThirdClass extends MyBaseClass
{
// this will inherit the constructor of MyBaseClass class
}
// In MyBaseClass constructor
$objct = new MyBaseClass();
// In MyBaseClass and AnotherClass constructor
$objct = new AnotherClass();
// In MyBaseClass constructor
$objct = new AnotherThirdClass();
?>
</body>
</html>