Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP6015 - An Introduction to Computer Programming : University of the West Indies COMP6015 An Introduction to Computer Programming Lecture 07.

Similar presentations


Presentation on theme: "COMP6015 - An Introduction to Computer Programming : University of the West Indies COMP6015 An Introduction to Computer Programming Lecture 07."— Presentation transcript:

1 COMP6015 - An Introduction to Computer Programming : University of the West Indies COMP6015 An Introduction to Computer Programming Lecture 07

2 COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to OOP in PHP Object-Oriented-Programming

3 COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to OOP in PHP In object-oriented programming: modules are organized into objects. Objects have: methods properties. From an abstract perspective: methods are things an object does properties are the characteristics of the object. From a programming perspective: methods are functions properties are variables.

4 COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to OOP in PHP Object-Oriented-Programming A class defines the attributes of objects. The class is frequently described as the TEMPLATE e.g. If you were baking a batch of cookies (objects), the template to the cookies would be the cookie cutter (class). The properties and methods of the class are called members. Expression may be qualified by data members or method members.

5 COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to OOP in PHP PHP – Object Model Inheritance: involves a parent-child relationship between classes. PHP supports single-inheritance, access-restricted, and overloadable object model. PHP allows for one parent per child. PHP supports restricting access to properties and methods. Members may be declared as private or public or protected PHP allows a child class to overload the members of its parent class.

6 COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to OOP in PHP Object-Oriented-Programming Every class definition: begins with the keyword class followed by a class name (any name that isn't a reserved word in PHP) followed by a pair of curly braces (which contains the definition of the classes members and methods)

7 COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to OOP in PHP <?php //define class for tracking users class User { //properties public $name; private $password, $lastLogin; //methods public function init($name, $password) { $this->name = $name; $this->password = $password; $this->lastLogin = time(); } // get the date of the last login function getLastLogin() { return ( date("M d Y", $this->lastLogin)); } } //End of class definition //create an instance $user1 = new User; $user1->init("Leon", "sdf123"); //get the last login date print ($user1->getLastLogin()."\n"); //print the user's name print ("$user1->name\n"); ?> Class Example

8 COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to OOP in PHP Object-Oriented-Programming Constructor Function Function executed immediately upon creating an object from that class. Name is __construct (with 2 preceding underscores) may have parameters and even default values. Can set all object’s properties in one statement

9 COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to OOP in PHP Object-Oriented-Programming Destructor Function Function executed just prior to object being destroyed. Name is __destruct (with 2 preceding underscores) may NOT have parameters. Can perform termination house-keeping Generally invoked by the operating system. May also be invoked by assigning NULL to the object

10 COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to OOP in PHP <?php //define class for tracking users class User { //properties public $name; private $password, $lastLogin; //methods public function __construct($name, $password) { $this->name = $name; $this->password = $password; $this->lastLogin = time(); } // get the date of the last login function getLastLogin() { return ( date("M d Y", $this->lastLogin)); } public function __destruct() { print (“Good-bye $user->name\n"); } } //End of class definition //create an instance $user1 = new User("Leon", "sf123"); //get the last login date print ($user1->getLastLogin()."\n"); //print the user's name print ("$user1->name\n"); ?> Class Example

11 COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to OOP in PHP <?php class Counter { //Attribute private static $count = 0; //Methods function __construct() { self::$count++; } function __destruct() { self::$count--; } function getCount() { return self::$count; } } //create one instance $c = new Counter(); //print 1 print($c->getCount(). “\n"); //create a second instance $c2 = new Counter(); //print 2 print($c->getCount(). "\n"); //destroy one instance $c2 = NULL; //print 1 print($c->getCount(). "\n"); ?> Class Constructor & Destructor

12 COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to OOP in PHP <?php class Room { public $name; function __construct($name = "unnamed") { $this->name = $name; } class House { //array of rooms public $room[]; } //create empty house $home = new house; //add some rooms $home->room[] = new Room("bedroom"); $home->room[] = new Room("kitchen"); $home->room[] = new room("bathroom"); //show the first room of the house print($home->room[0]->name); ?> Class: Object Containing Objects

13 COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to OOP in PHP Object-Oriented-Programming  If a class extends another, the properties and methods of all ancestor classes are available in the child class despite not being declared explicitly.  As mentioned previously, inheritance is very powerful.  If you wish to access an inherited property, you may simply refer to it as you would any other local property.  Alternatively, you may specify a specific namespace using the :: operator.

14 COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to OOP in PHP Object-Oriented-Programming PHP recognizes two special namespaces within objects: The parent namespace refers to the immediate ancestor class. The self namespace refers to the current class.

15 COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to OOP in PHP Object-Oriented-Programming Extending a class: Classes are extended through inheritance. A parent class is extended through a child. FORMAT: class extends { }

16 COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to OOP in PHP <?php class Animal //Parent class { public $blood; public $name; public function __construct($blood, $name=NULL) { $this->blood = $blood; if ( $name) { $this->name = $name; } class Mammal extends Animal //Mammal is child of Animal { public $furColor; public $legs; Class: Extending (inheritance)

17 COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to OOP in PHP function __construct($furColor, $legs, $name=NULL) { parent::__construct("warm", $name); $this->furColor = $furColor; $this->legs = $legs; } class Dog extends Mammal //Dog is child of Mammal { function __construct($furColor, $name) { parent::__construct($furColor, 4, $name); self::bark(); } function bark() { print("$this->name says 'woof!'"); } $d = new Dog("Black and Tan", "Angus"); ?> Class: Extending (inheritance)


Download ppt "COMP6015 - An Introduction to Computer Programming : University of the West Indies COMP6015 An Introduction to Computer Programming Lecture 07."

Similar presentations


Ads by Google