Visibility public, private, protected... Properties & Methodsclass Person { private $name ; //public, protected private $age ; echo $this->name... Getter & Setterclass Person { private $
Trang 1OBJECT ORIENTED PROGRAMMING
PHÁT TRIỂN ỨNG DỤNG WEB
Trang 21 Class
2 Visibility
3 Properties & Methods
4 Getter & Setter
Trang 4Visibility (public, private, protected)
Trang 5Properties & Methods
class Person
{
private $name ; //public, protected
private $age ;
echo $this->name " is " $this->age " years old!";
}
}
Trang 6Create objects
(Create a new instance of the class)
− For example:
$person = new PerSon();
$person2 = new PerSon();
6
Trang 7Getter & Setter
class Person
{
private $name ;
private $age ;
public function getName(){
return $this-> name ;
}
public function setName($name){
$this-> name = $name;
}
public function getAge(){
return $this-> age ;
}
public function setAge($age){
class Person
{
private String name ;
private int age ;
public String getName(){
return name ;
}
public void setName(String name ){
this name = name ;
Trang 8class Person {
private $name ;
private $age ;
public function getName(){
return $this-> name ; }
public function setName($name){
$this-> name = $name;
}
public function getAge(){
return $this-> age ; }
public function setAge($age){
$this-> age = $age;
}
public function show(){
echo $this-> name " is " $this-> age " years old!" ; }
$p->setName( "Nguyễn Văn A" );
$p->setAge(18);
echo $p->getName() " is " $p->getAge() " years old." ;
//echo " {$p->getName()} is {$p->getAge()} years old." ;
$p->show();
Ex: Person.php
Trang 10get() method
10
class GetName {
public $type = 'chocolate' ;
public $choctype = array (
'milk' => 0, 'dark' => 1, 'plain' => 2 );
public function wrap() {
echo 'Its a wrap';
}
public function get($index) {
echo '$choctype property with index of: ' $index '<br />';
return $this->choctype [$index];
}
}
$candy = new GetName ();
// set a non existant property
echo 'Value of property is: ' $candy->milk; $choctype property with index of: milk
Value of property is: 0
Trang 12$this-> name = $name;
$this-> age = $age;
}
}
}
$p = new Person( "Nguyễn Trần Lê" , 18);
$p->show();
Trang 13public function destruct() {
echo "Bye bye!" ;
//
}
$p = new Person( "Nguyễn Trần Lê" , 18);
Trang 1414
class ParentClass {
// Method code here
}
}
class ChildClass extends ParentClass {
// For ChildClass objects, this method is called // instead of the parent class's MyMethod()
}
}
Trang 15Calling a parent method from a child method
parent ::myMethod();
parent::show();
parent:: construct();
Example:
Trang 16public function construct($name, $age, $salary){
parent:: construct($name, $age); // Call construct() parent
$this-> salary = $salary;
}
public function getSalary(){
return $this-> $salary;
public function display(){
echo " Name: " $this->getName() "<br>" ;
echo " Age: " $this->getAge() "<br>" ;
echo "Salary: " $this-> salary ; }
Trang 17Visibility (Example 1)
Property Visibility
class MyClass {
public $public = 'Public' ;
protected $protected = 'Protected' ;
private $private = 'Private' ;
function printHello () {
echo $this -> public ;
echo $this -> protected ;
echo $this -> private ;
}
}
$obj = new MyClass ();
echo $obj -> public ; // Works
echo $obj -> protected ; // Fatal Error
Trang 18Visibility (Example 2)
Property Visibility
18
class MyClass2 extends MyClass {
// We can redeclare the public and protected method, but not private
public $public = 'Public2' ;
protected $protected = 'Protected2' ;
function printHello () {
echo $this -> public ;
echo $this -> protected ;
echo $this -> private ;
}
}
$obj2 = new MyClass2 ();
echo $obj2 -> public ; // Works
echo $obj2 -> protected ; // Fatal Error
echo $obj2 -> private ; // Undefined
$obj2 -> printHello (); // Shows Public2, Protected2, Undefined
Trang 19Visibility (Example 3)
Method Visibility
class MyClass {
// Declare a public constructor
public function construct () { }
// Declare a public method
public function MyPublic () { }
// Declare a protected method
protected function MyProtected () { }
// Declare a private method
private function MyPrivate () { }
Trang 20$myclass2 = new MyClass2 ;
$myclass2 -> MyPublic (); // Works
$myclass2 -> Foo2 (); // Public and Protected work, not Private
Trang 21private function testPrivate () { echo "Foo::testPrivate\n" ; }
}
$myFoo = new Foo ();
$myFoo -> test (); // Bar::testPrivate
// Foo::testPublic
Trang 22Abstract class
− An abstract class is a class that cannot be instantiated on its
own
22
abstract class Mathematics {
/*** child class must define these methods ***/
abstract protected function getMessage();
abstract protected function add($num);
Trang 23Abstract class
require_once 'Mathematics.php' ;
class myMath extends Mathematics {
return "The anwser is: " ;
// A new instance of myMath
$myMath = new MyMath ();
$myMath->showMessage ();
Trang 24− An interface declares one or more methods.
− Must be implemented by any class that implements the
interface.
24
interface MyInterface {
public function aMethod();
public function anotherMethod();
}
class MyClass implements MyInterface {
public function aMethod() {
// (code to implement the method)
}
public function anotherMethod() {
// (code to implement the method)
} }
Trang 25interface Persistable {
public function save();
public function load();
public function delete();
$this-> username = $username;
$this-> location = $location;
$this-> homepage = $homepage;
}
public function getUsername() {
return $this-> username ;
}
public function getLocation() {
return $this-> location ;
}
public function save() {
echo "Saving member to database<br>" ;
}
public function load() {
echo "Loading member from database<br>" ;
}
public function delete () {
echo "Deleting member from database<br>" ;
} }
$m = new Member ( "Aha" , "VN" , "No page" );
echo $m->getUsername() "<br>" ;
$m->load();
Trang 26class Topic implements Persistable {
private $subject ;
private $author ;
private $createdTime ;
public function construct( $subject, $author ) {
$this-> subject = $subject;
$this-> author = $author;
$this-> createdTime= time();
}
public function showHeader() {
$createdTimeString = date( 'l jS M Y h:i:s A' , $this-> createdTime );
$authorName = $this-> author ->getUsername();
echo " $this-> subject (created on $createdTimeString by $authorName )<br>" ; }
public function save() {
echo "Saving topic to database<br>" ; }
public function load() {
echo "Loading topic from database<br>" ; }
public function delete () {
echo "Deleting topic from database<br>" ; }
}
Trang 27require_once 'Member.php' ;
require_once 'Topic.php' ;
$aMember = new Member ( "TUI" , "Viet Nam" , "http://example.com" );
echo $aMember->getUsername () " lives in " $aMember->getLocation () "<br>" ;
$aMember->save ();
$aTopic = new Topic ( "PHP is Great" , $aMember );
$aTopic->showHeader ();
$aTopic->save ();
TUI lives in Viet Nam
Saving member to database
PHP is Great (created on Saturday 17th Dec 2016 03:00:07 AM by TUI)
Saving topic to database
Test.php
Trang 28$member = new Member ("TUI", "Viet Nam", "No page");
echo "Created object: ";
print_r ( $member );
Loaded Member.php
Created object: Member Object ( [username:Member:private] => TUI
[location:Member:private] => Viet Nam [homepage:Member:private] => No page )
Trang 29Anonymous functions
− Anonymous functions have no name
// Assign an anonymous function to a variable
$makeGreeting = function ($name, $timeOfDay) {
return ( 'Good ' $timeOfDay ', ' $name);
};
// Call the anonymous function
echo $makeGreeting ( 'Tom' , 'morning' ) '' ;
echo $makeGreeting ( 'Jerry' , 'afternoon' ) '' ;
Good morning, Tom
Good afternoon, Jerry
Trang 30− A closure is an anonymous function that can access variables
imported from the outside scope without using any global
Trang 31− Namespaced code is defined using a singlenamespace
keyword at the top of your PHP file
− It must be the first command (with the exception ofdeclare)
and no non-PHP code, HTML, or white-space
// define this code in the 'MyProject' namespace
namespace MyProject;
// code
Trang 32namespace test;
include 'Cat.php' ;
include 'Dog.php' ;
include 'Animal.php' ;
use foo as cat;
use bar as dog;