Lecture 17 - PHP-Object-Orientation
Lecture 17 - PHP-Object-Orientation
http://xkcd.com/327/
Encapsulation, Inheritance, Polymorphism
• Data members
• Methods
Object Orientation
function getFirstname() {
return $this->strFirstname;
} Methods
function getSurname() {
return $this->strSurname;
}
}
// outside the class definition
$obj = new Person; // an object of type Person
echo "<p>Firstname: " . $obj->getFirstname() . "</p>";
echo "<p>Surname: " . $obj->getSurname() . "</p>";
?>
Example16-1.php
Encapsulation
Data members are normally set inaccessible from outside
the class (as well as certain types of methods) protecting
them from the rest of the script and other classes.
e.g.
<?php
....
$dogRobot = new Robot();
$dogRobot ->crawlWeb();
$dogRobot -> play();
echo $dogRobot ->talk();
...
?>
Inheritance
New classes can be defined very similar to existing ones.
All we need to do is specify the differences between the
new class and the existing one.
function setSurname($strSurname) {
$this->strSurname = $strSurname;
}
By default, without
the access specifiers,
class members are
defined public.
Private Access Specifier
class MyClassName{
private $strFirstName;
}
protected $strFirstName;
protected function getFirstName(){
}
Inherited protected class members –
} accessible inside a derived class
Visibility of protected class
members outside the class definition
– protected class members are
inaccessible.
PROPERTY DECLARATION
Example: Access Specifiers
<?php
class MyClass {
public $public = 'Public';
protected $protected = 'Protected'; //protected for public use, but accessible in a derived class
private $private = 'Private';
function printHello() {
echo $this->public;
echo $this->protected;
echo $this->private;
}
}
// outside the class definition
$obj = new MyClass();
echo $obj->public; // Works
echo $obj->protected; // Fatal Error
echo $obj->private; // Fatal Error
$obj->printHello(); // Shows Public, Protected and Private
//...
http://php.net/manual/en/language.oop5.visibility.php
Example: Access Specifiers
<?php a Derived class
//...
class MyClass2 extends MyClass
{
// We can redeclare the public and protected method, but not private
// protected – „protected‟ for public use, but accessible in a derived class
protected $protected = 'Protected2';
function printHello()
{
echo $this->public;
echo $this->protected;
echo $this->private;
}
}
// outside the class definition
$obj2 = new MyClass2();
echo $obj2->public; // Works
echo $obj2->private; // Undefined
echo $obj2->protected; // Fatal Error
$obj2->printHello(); // Shows Public, Protected2, Undefined
?> http://php.net/manual/en/language.oop5.visibility.php
METHOD DECLARATION
Example: Method Declaration
<?php //OUTSIDE THE CLASS DEFINITION...
class MyClass
{ $myclass = new MyClass;
// Declare a public constructor
public function __construct() { }
$myclass->MyPublic(); // Works
$myclass->MyProtected(); // Fatal Error
$myclass->MyPrivate(); // Fatal Error
// Declare a public method
public function MyPublic() { }
$myclass->Foo(); // Public, Protected and Private work
//...
// Declare a protected method
protected function MyProtected() { }
// This is public
function Foo()
{
$this->MyPublic();
$this->MyProtected();
$this->MyPrivate();
}
}
http://php.net/manual/en/language.oop5.visibility.php
Example: Method Declaration
<?php class MyClass2 extends MyClass
class MyClass {
{ // This is public
// Declare a public constructor
public function __construct() { }
function Foo2()
{
// Declare a public method $this->MyPublic();
public function MyPublic() { } $this->MyProtected();
$this->MyPrivate(); // Fatal Error
// Declare a protected method }
protected function MyProtected() { } }
// Declare a private method
$myclass2 = new MyClass2;
private function MyPrivate() { }
// This is public
$myclass2->MyPublic(); // Works
$myclass2->Foo2(); // Public and Protected work, not Private
function Foo()
{
$this->MyPublic();
$this->MyProtected();
$this->MyPrivate();
}
}
http://php.net/manual/en/language.oop5.visibility.php
Example: Method Declaration
<?php
class Bar
{
public function test() {
$this->testPrivate();
$this->testPublic();
}
$myFoo = new foo();
public function testPublic() {
echo "Bar::testPublic\n"; $myFoo->test(); // Bar::testPrivate
}
$test->baz(new Test('other'));
?>
http://php.net/manual/en/language.oop5.visibility.php
Creating objects
•Instantiate classes using new keyword
–$myCart= new ShoppingCart(“Charlie”);
Constructors
–In earlier versions of PHP (< PHP5.3.3) Same as the
name of the class. This no longer holds!
– (PHP5 only) declared as
• public function __construct(…)
Destructors
–Declared as
–public function __destruct()
22 Sept. 2010
Latest in PHP5.3.3
<?php
namespace Foo;
class Bar {
public function Bar() {
// treated as constructor in PHP 5.3.0-5.3.2
// treated as regular method in PHP 5.3.3
}
}
?>
Constructors
A constructor is a function that does
initializations when the class is instantiated
function __construct($intNumber, $strName){
$this->set_intNumber($intNumber);
$this->set_strName($strName);
$this->printInit();//use this method
}
Constructors
Default arguments
function __construct ($strName = “A”, $intNumber=0) {
$this->set_intNumber($int_Number);
$this->set_strName($str_Name);
}
Instantiating a class without parameters will
make use of the default values
Another Example: Constructors
<?php
class vehicle {
private $strDescription;
function getDescription() {
return $this->strDescription;
}
function setDescription($strDescription) {
$this->strDescription = $strDescription;
}
require_once("vehicle.php");
?>
example16-7.php
Destructors
Called when objects are destroyed – free up
memory
e.g.:
function __destruct () {
echo “freeing up memory, destroying this object... <br>”;
}
<?php
$cart1 = new ShoppingCart(“Joe Bloggs”);
$cart1->addItem("10", 1);
• require_once(….)
Includes file specified only if it has not already been
included, terminates on errors
• include_once(….)
Includes file specified only if it has not already been
included, gives warning on errors
Really useful but would require you to write a long list of include() or require() statements
at the beginning of each script, one for each class. In PHP5, this is no longer
necessary. You may define an __autoload function!
Example16-6.php
function __autoload()
• The function is invoked automatically each time a class is
required but has not been defined.
function __autoload($class_name) {
require_once $class_name . '.php';
}
Example16-7.php
function __autoload()
<?php
function __autoload($class_name) {
require_once $class_name . '.php';
} Class definition comes from
another file.
Example16-7.php
Exceptions
// with reflection
New in PHP, not properly
$f = new ReflectionClass("Foo"); documented yet!
http://en.wikipedia.org/wiki/Reflection_(computer_science)
http://nz2.php.net/manual/en/reflectionclass.newinstance.php
For a complete reference
http://www.php.net