oop in php
DESCRIPTION
Object orientation is probably the area that has been subject to the most significant and far-reaching changes with the advent of PHP 5. Rather than making things incompatible with previous versions of the language, however, they enhance PHP 4’s meagre OOP offerings and make PHP 5 a fully functional object-oriented language.TRANSCRIPT
![Page 1: OOP in PHP](https://reader033.vdocuments.mx/reader033/viewer/2022061218/54b779154a795918738b46cb/html5/thumbnails/1.jpg)
OBJECT ORIENTED
PROGRAMMING IN PHP
HENRY OSBORNE
![Page 2: OOP in PHP](https://reader033.vdocuments.mx/reader033/viewer/2022061218/54b779154a795918738b46cb/html5/thumbnails/2.jpg)
FUNDAMENTALS
•Classes
•Encapsulation
•Objects
![Page 3: OOP in PHP](https://reader033.vdocuments.mx/reader033/viewer/2022061218/54b779154a795918738b46cb/html5/thumbnails/3.jpg)
CLASS DECLARATION
The basic declaration of a class is very simple:
class myClass {
// Class contents go here
}
![Page 4: OOP in PHP](https://reader033.vdocuments.mx/reader033/viewer/2022061218/54b779154a795918738b46cb/html5/thumbnails/4.jpg)
OBJECT INSTANTIATION
This is done by using the new construct:
$myClassInstance = new myClass();
An object is always passed by reference rather than by value.
$myClassInstance = new myClass();
$copyInstance = $myClassInstance();
![Page 5: OOP in PHP](https://reader033.vdocuments.mx/reader033/viewer/2022061218/54b779154a795918738b46cb/html5/thumbnails/5.jpg)
CLASS INHERITANCEAllows a class to extend another class, essentially adding new methods and properties, as well as overriding existing ones as needed.class a {
function test(){
echo "a::test called";}function func(){
echo "a::func called";}
}class b extends a {
function test(){
echo "b::test called";}
}
class c extends b {function test(){
parent::test();}
}class d extends c {
function test(){
b::test();}
}$a = new a();$b = new b();$c = new c();$d = new d();
![Page 6: OOP in PHP](https://reader033.vdocuments.mx/reader033/viewer/2022061218/54b779154a795918738b46cb/html5/thumbnails/6.jpg)
METHODS AND PROPERTIESMethods are declared just like traditional functions:
class myClass {
function myFunction() {
echo "You called myClass::myFunction";
}
}
From outside the scope of a class, its methods are called using the indirection operator ->:
$obj = new myClass();
$obj->myFunction();
![Page 7: OOP in PHP](https://reader033.vdocuments.mx/reader033/viewer/2022061218/54b779154a795918738b46cb/html5/thumbnails/7.jpg)
METHODS AND PROPERTIESclass myClass {
function myFunction($data) {
echo "The value is $data";
}
function callMyFunction($data) {
// Call myFunction()
$this->myFunction($data);
}
}
$obj = new myClass();
$obj->callMyFunction(123);
![Page 8: OOP in PHP](https://reader033.vdocuments.mx/reader033/viewer/2022061218/54b779154a795918738b46cb/html5/thumbnails/8.jpg)
CONSTRUCTORSclass foo {
function __construct()
{
echo __METHOD__;
}
function foo()
{
// PHP 4 style constructor
}
}
new foo();
![Page 9: OOP in PHP](https://reader033.vdocuments.mx/reader033/viewer/2022061218/54b779154a795918738b46cb/html5/thumbnails/9.jpg)
DESTRUCTORSclass foo {
function __construct()
{
echo __METHOD__ . PHP_EOL;
}
function __destruct()
{
echo __METHOD__;
}
}
new foo();
This code will display:
foo::__construct
foo::__destruct
![Page 10: OOP in PHP](https://reader033.vdocuments.mx/reader033/viewer/2022061218/54b779154a795918738b46cb/html5/thumbnails/10.jpg)
VISIBILITYpublic The resource can be accessed from any scope.
protected The resource can only be accessed from within the class where it is defined and its descendants.
private The resource can only be accessed from within the class where it is defined.
final The resource is accessible from any scope, but cannot be overridden in descendant classes.
![Page 11: OOP in PHP](https://reader033.vdocuments.mx/reader033/viewer/2022061218/54b779154a795918738b46cb/html5/thumbnails/11.jpg)
DECLARING AND ACCESSING PROPERTIESclass foo {
public $bar;
protected $baz;
private $bas;
public $var1 = "Test"; // String
public $var2 = 1.23; // Numeric value
public $var3 = array (1, 2, 3);
}
![Page 12: OOP in PHP](https://reader033.vdocuments.mx/reader033/viewer/2022061218/54b779154a795918738b46cb/html5/thumbnails/12.jpg)
CONSTANTS, STATIC METHODS AND PROPERTIESclass foo {
static $bar = "bat";
public static function baz()
{
echo "Hello World";
}
}
$foo = new foo();
$foo->baz();
echo $foo->bar;
Hello WorldPHP Strict Standards: Accessing static property foo::$bar as non static in PHPDocument1 on line 17Strict Standards: Accessing static property foo::$bar as non static in PHPDocument1 on line 1
![Page 13: OOP in PHP](https://reader033.vdocuments.mx/reader033/viewer/2022061218/54b779154a795918738b46cb/html5/thumbnails/13.jpg)
CLASS CONSTANTS
class foo {
const BAR = "Hello World";
}
echo foo::BAR;
![Page 14: OOP in PHP](https://reader033.vdocuments.mx/reader033/viewer/2022061218/54b779154a795918738b46cb/html5/thumbnails/14.jpg)
INTERFACES AND ABSTRACT CLASSES•An abstract class essentially defines the basic skeleton of a specific type of encapsulated entity.
•Interfaces, on the other hand, are used to specify an API that a class must implement.
![Page 15: OOP in PHP](https://reader033.vdocuments.mx/reader033/viewer/2022061218/54b779154a795918738b46cb/html5/thumbnails/15.jpg)
EXCEPTIONS• Exceptions provide an error control mechanism that is more fine-grained than traditional PHP fault handling, and that allows for a much greater degree of control.
• Key differences between “regular” PHP errors and exceptions:
• Exceptions are objects, created (or “thrown”) when an error occurs
• Exceptions can be handled at different points in a script’s execution, and different types of exceptions can be handled by separate portions of a script’s code
• All unhandled exceptions are fatal
• Exceptions can be thrown from the __construct method on failure
• Exceptions change the flow of the application
![Page 16: OOP in PHP](https://reader033.vdocuments.mx/reader033/viewer/2022061218/54b779154a795918738b46cb/html5/thumbnails/16.jpg)
OBJECT ORIENTED
PROGRAMMING IN PHP