Object Inheritance

Examples:
Inheritance Example
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
<?php
 
class Foo
{
    public function printItem($string)
    {
        echo 'Foo: ' $string . PHP_EOL;
    }
     
    public function printPHP()
    {
        echo 'PHP is great.' . PHP_EOL;
    }
}
 
class Bar extends Foo
{
    public function printItem($string)
    {
        echo 'Bar: ' $string . PHP_EOL;
    }
}
 
$foo new Foo();
$bar new Bar();
$foo->printItem('baz'); // Output: 'Foo: baz'
$foo->printPHP();       // Output: 'PHP is great' 
$bar->printItem('baz'); // Output: 'Bar: baz'
$bar->printPHP();       // Output: 'PHP is great'
 
?>
doc_php
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.