Bài giảng Phát triển ứng dụng web - Bài 5: Object oriented programming - Nguyễn Hữu Thể

PHÁT TRIỂN ỨNG DỤNG WEB  
OBJECT ORIENTED PROGRAMMING  
Nguyn Hu Thể  
Content  
1. Class  
2. Visibility  
3. Properties & Methods  
4. Getter & Setter  
5. Create objects  
6. Constructor  
7. Destructor  
8. Inheritance  
9. Abstract class  
10. Interfaces  
11. Autoloading classes  
12. Anonymous functions  
13. Closures  
14. Namespace  
2
Class  
class ClassName  
{
// Properties  
// Methods  
}
3
Visibility (public, private, protected)  
Three levels:  
public  
private  
protected  
By default, all class members are public.  
4
Properties & Methods  
class Person  
{
private $name; //public, protected  
private $age;  
public function show(){  
echo $this->name . " is " . $this->age . " years old!";  
}
}
5
Create objects  
(Create a new instance of the class)  
Using the new keyword:  
new ClassName()  
For example:  
$person = new PerSon();  
$person2 = new PerSon();  
6
Getter & Setter  
JAVA  
PHP  
class Person  
class Person  
{
{
private String name;  
private $name;  
private int age;  
private $age;  
public String getName(){  
public function getName(){  
return name;  
return $this->name;  
}
}
public void setName(String name){  
public function setName($name){  
this.name = name;  
$this->name = $name;  
}
}
public int getAge(){  
public function getAge(){  
return age;  
return $this->age;  
}
}
public void setAge(int age){  
public function setAge($age){  
this.age = age;  
$this->age = $age;  
}
}
}
}
7
class Person{  
private $name;  
Ex: Person.php  
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 = new Person();  
}
$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();  
8
__set() method  
class SetName {  
public function __set($variable, $value) {  
// echo $variable;  
echo "My " . $variable . " is " . $value . "\n";  
}
}
$obj = new SetName ();  
$obj->Name = ‘Tom';  
$obj->Name = ‘Jerry';  
My Name is Tom  
My Name is Jerry  
9
__get() method  
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  
Constructor  
public function __construct() {  
//...  
}
public function __construct($name, $age) {  
//...  
}
11  
Constructor  
class Person{  
private $name;  
private $age;  
public function __construct($name, $age) {  
$this->name = $name;  
$this->age = $age;  
}
public function show(){  
echo $this->name . " is " . $this->age . " years old!";  
}
}
$p = new Person("Nguyễn Trần Lê", 18);  
$p->show();  
12  
Destructor  
public function __destruct() {  
//...  
}
Example:  
public function __destruct() {  
echo "Bye bye!";  
//...  
}
Using:  
$p = new Person("Nguyễn Trần Lê", 18);  
unset( $p ); // or exit: call __destruct() object  
$p->show(); // Object not found  
13  
Inheritance  
class ParentClass {  
public function myMethod() {  
// Method code here  
}
}
class ChildClass extends ParentClass {  
public function myMethod() {  
// For ChildClass objects, this method is called  
// instead of the parent class's MyMethod()  
}
}
14  
Calling a parent method from a child method  
parent::myMethod();  
Example:  
parent::show();  
parent::__construct();  
15  
require_once 'Person.php';  
class Employee extends Person{
private $salary;  
public function __construct($name, $age, $salary){  
parent::__construct($name, $age); // Call __construct() parent  
$this->salary = $salary;  
}
public function getSalary(){  
return $this-> $salary;  
}
// Override  
public function show(){  
parent::show();  
echo " Salary: " . $this->salary;  
}
public function display(){  
echo " Name: " . $this->getName() . "<br>";  
echo " Age: " . $this->getAge() . "<br>";  
echo "Salary: " . $this->salary;  
$e = new Employee("Nguyễn Lê", 20, 200);  
$e->show();  
}
}
Nguyễn Lê is 20 years old! Salary: 200  
Visibility (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  
echo $obj->private; // Fatal Error  
$obj->printHello(); // Shows Public, Protected and Private  
17  
Visibility (Example 2)  
Property Visibility  
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  
18  
Visibility (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() { }  
// This is public  
function Foo() {  
$this->MyPublic();  
$myclass = new MyClass;  
$myclass->MyPublic(); // Works  
$myclass->MyProtected(); // Fatal Error  
$myclass->MyPrivate(); // Fatal Error  
$myclass->Foo(); // Public, Protected and Private work  
$this->MyProtected();  
$this->MyPrivate();  
}
19  
}
Visibility (Example 4)  
Method Visibility  
class MyClass2 extends MyClass  
{
// This is public  
function Foo2()  
{
$this->MyPublic();  
$this->MyProtected();  
$this->MyPrivate(); // Fatal Error  
}
}
$myclass2 = new MyClass2;  
$myclass2->MyPublic(); // Works  
$myclass2->Foo2(); // Public and Protected work, not Private  
20  
Tải về để xem bản đầy đủ
pdf 32 trang baolam 10/05/2022 5240
Bạn đang xem 20 trang mẫu của tài liệu "Bài giảng Phát triển ứng dụng web - Bài 5: Object oriented programming - Nguyễn Hữu Thể", để tải tài liệu gốc về máy hãy click vào nút Download ở trên

File đính kèm:

  • pdfbai_giang_phat_trien_ung_dung_web_bai_5_object_oriented_prog.pdf