Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
ICT 5 Web Development - Chapter 5: OOP in PHP Creating an Object, Accessing attributes and methods, Building a classing, Introspection, Syntax to declare a Class, Autoloading class. | Vietnam and Japan Joint ICT HRD Program ObjectObject-Oriented Programming (OOP) Object: Object: Instance (occurrance) of a class Classes/Objects encapsulates th i d t Cl /Obj t l t their data (called attributes) and behaviour (called attributes) methods) methods) Inheritance: Define Inheritance: Define a new class by saying that it's like an existing class, but with certain new or changed attributes and methods. ICT 5 Web Development Chapter 5. OOP in PHP Nguyen Thi Thu Trang – The old class: superclass/parent/base class class: superclass/parent/ – The new class: subclass/child/derived class The class: subclass/child/derived trangntt@soict.hut.edu.vn 2 PHP 5 Content Single-inheritance ingleAccess-restricted ccessOverloadable Object ~ pass-by-reference pass-by- 1. Creating an Object 2. Accessing attributes and methods 3. Building a class Building 4. Introspection 3 4 1 1. Creating an Object Content 1. Creating an Object 2. Accessing attributes and methods 3. Building a class Building 4. Introspection Syntax: – $object = new Class([agrs]); Class([agrs]); E.g.: – $obj1= new User(); User(); – $obj2 = new User('Fred', "abc123"); //args – $obj3 = new ‘User'; // does not work – $class = ‘User'; $obj4 new $class; //ok User ; $obj4= User + name - password - lastLogin + getLastLogin() + setPassword(pass) 5 2. Accessing Attributes and Methods 6 Content 1. Creating an Object 2. Accessing attributes and methods 3. Building a class Building 4. Introspection Syntax: Using -> – $object->attribute name $object- attribute_ attribute_name – $object->method_name([arg, . ]) $object- method_name([arg, ]) E.g. // attribute access $obj1$obj1->name = “Micheal"; print("User name is " . $obj1->name); $obj1$obj1$obj1->getLastLogin( ); // method call // method call with args $obj1$obj1->setPassword("Test4"); 7 8 2 Rules for PHP Identifiers 3.1. Syntax to declare a Class class ClassName [extends BaseClass]{ lassN aseClass]{ [[var] access $attribute [ = value ]; .