Classes can inherit the methods and properties of another class using the
extends
keyword. For instance, to create a second class that extends MyClass
and adds a method, you would add the following to your test file:<?php
class
MyClass
{
public
$prop1
=
"I'm a class property!"
;
public
function
__construct()
{
echo
'The class "'
,
__CLASS__
,
'" was initiated!<br />'
;
}
public
function
__destruct()
{
echo
'The class "'
,
__CLASS__
,
'" was destroyed.<br />'
;
}
public
function
__toString()
{
echo
"Using the toString method: "
;
return
$this
->getProperty();
}
public
function
setProperty(
$newval
)
{
$this
->prop1 =
$newval
;
}
public
function
getProperty()
{
return
$this
->prop1 .
"<br />"
;
}
}
class
MyOtherClass
extends
MyClass
{
public
function
newMethod()
{
echo
"From a new method in "
.
__CLASS__
.
".<br />"
;
}
}
// Create a new object
$newobj
=
new
MyOtherClass;
// Output the object as a string
echo
$newobj
->newMethod();
// Use a method from the parent class
echo
$newobj
->getProperty();
?>
Upon reloading the test file in your browser, the following is output:
The
class
"MyClass"
was initiated!
From a
new
method in MyOtherClass.
I'm a
class
property!
The
class
"MyClass"
was destroyed.
Overwriting Inherited Properties and Methods
To change the behavior of an existing property or method in the new
class, you can simply overwrite it by declaring it again in the new
class:
<?php
class
MyClass
{
public
$prop1
=
"I'm a class property!"
;
public
function
__construct()
{
echo
'The class "'
,
__CLASS__
,
'" was initiated!<br />'
;
}
public
function
__destruct()
{
echo
'The class "'
,
__CLASS__
,
'" was destroyed.<br />'
;
}
public
function
__toString()
{
echo
"Using the toString method: "
;
return
$this
->getProperty();
}
public
function
setProperty(
$newval
)
{
$this
->prop1 =
$newval
;
}
public
function
getProperty()
{
return
$this
->prop1 .
"<br />"
;
}
}
class
MyOtherClass
extends
MyClass
{
public
function
__construct()
{
echo
"A new constructor in "
.
__CLASS__
.
".<br />"
;
}
public
function
newMethod()
{
echo
"From a new method in "
.
__CLASS__
.
".<br />"
;
}
}
// Create a new object
$newobj
=
new
MyOtherClass;
// Output the object as a string
echo
$newobj
->newMethod();
// Use a method from the parent class
echo
$newobj
->getProperty();
?>
This changes the output in the browser to:
A
new
constructor in MyOtherClass.
From a
new
method in MyOtherClass.
I'm a
class
property!
The
class
"MyClass"
was destroyed.
Preserving Original Method Functionality While Overwriting Methods
To add new functionality to an inherited method while keeping the original method intact, use the
parent
keyword with the scope resolution operator (::
):<?php
class
MyClass
{
public
$prop1
=
"I'm a class property!"
;
public
function
__construct()
{
echo
'The class "'
,
__CLASS__
,
'" was initiated!<br />'
;
}
public
function
__destruct()
{
echo
'The class "'
,
__CLASS__
,
'" was destroyed.<br />'
;
}
public
function
__toString()
{
echo
"Using the toString method: "
;
return
$this
->getProperty();
}
public
function
setProperty(
$newval
)
{
$this
->prop1 =
$newval
;
}
public
function
getProperty()
{
return
$this
->prop1 .
"<br />"
;
}
}
class
MyOtherClass
extends
MyClass
{
public
function
__construct()
{
parent::__construct();
// Call the parent class's constructor
echo
"A new constructor in "
.
__CLASS__
.
".<br />"
;
}
public
function
newMethod()
{
echo
"From a new method in "
.
__CLASS__
.
".<br />"
;
}
}
// Create a new object
$newobj
=
new
MyOtherClass;
// Output the object as a string
echo
$newobj
->newMethod();
// Use a method from the parent class
echo
$newobj
->getProperty();
?>
This outputs the result of both the parent constructor and the new class's constructor:
The
class
"MyClass"
was initiated!
A
new
constructor in MyOtherClass.
From a
new
method in MyOtherClass.
I'm a
class
property!
The
class
"MyClass"
was destroyed.
Post a Comment
Silahkan anda tulis komentar di bawah ini !