1 / 19

Object-Oriented PHP (1)

Object-Oriented PHP (1). the object-oriented approach. simplicity: software objects model real world objects, so the complexity is reduced and the program structure is very clear;

gdouthitt
Download Presentation

Object-Oriented PHP (1)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Object-Oriented PHP (1)

  2. the object-oriented approach • simplicity:software objects model real world objects, so the complexity is reduced and the program structure is very clear; • modularity:each object forms a separate entity whose internal workings are decoupled from other parts of the system; • modifiability:it is easy to make minor changes in the data representation or the procedures in an OO program. Changes inside a class do not affect any other part of a program, since the only public interface that the external world has to a class is through the use of methods; • extensibility:adding new features or responding to changing operating environments can be solved by introducing a few new objects and modifying some existing ones; • maintainability: objects can be maintained separately, making locating and fixing problems easier; • re-usability: objects can be reused in different programs. strong cohesion loose coupling abstraction encapsulation

  3. the object-oriented approach • many modern programming languages either require (java, ruby, smalltalk etc.) or support (php, perl, F# etc.) the oo approach to software development. • object-oriented development attempts to use the classifications, relationships, and properties of the “objects” in the system to aid in program development and code reuse. • the oo approach can be compared and contrasted with other approaches to programming – e.g. imperative, procedural, functional, logical, event-driven, declarative, aspect-oriented ….

  4. classes and objects (1) • in oo terminology, an “object” can represent something “real” (e.g. an user) or something “conceptual” (e.g. a file) • oo software is designed and built as a set of self-contained objects that have both attributes (properties) and operations (behaviours) that interact with other objects (with attributes and behaviours) • a major advantage of o-o software is its capability to support and encourage encapsulation – or data hiding. Essentially, access to the data within an object is only available via an objects operations – known as the objects interface

  5. classes and objects (2) • an objects functionality is bound to the data it uses • it is possible to alter the internals of an object to improve performance, add new features etc. without altering its interface (how it is accessed) • hence, an oo approach can help manage complexity; increase code reusability and thereby reduce maintenance costs • objects can be grouped into “classes” – a class can be thought of as a template for objects of that type

  6. creating classes, attributes & operations (1) • structure of a class : a minimal class definition class Classname { } • however, to be useful a class needs attributes (properties) and operations (methods) • attributes (properties) are created using keywords that match their visibility : public, private or protected • the following code creates a class with three public attributes class Person { public $name; public $dob; public $gender; }

  7. creating classes, attributes & operations (2) • operations or methods are created by declaring functions within the class definition • the following code extends the Person class with two operations (methods) set_name and get_name • the first method takes 1 parameter and the second takes none but operations can take 0 to any number of parameters class Person { function set_name($name) { $this->name = $name; } function get_name(){ return $this->name; } }

  8. constructors • most classes have a special operation called a constructor - which is always called when an object of that class is created • a constructor is like other operations (methods) but has the special name construct() [note the double underscores __ ] • the following code re-defines the Person class with a constructor class Person { function __construct($n, $d, $g){ $this->name = $n; $this->dob = $d; $this->gender = $g; } }

  9. instantiating classes • after a class has been declared - an object can be created of that class • such an object is an “instantiation” of that class (a particular individual that is a member of that class) • objects are created using the new keyword • when the object is created any parameters required by the _construct method (operation) can be provided - the following code uses our Person class passing it the expected parameters to creates three Person objects …

  10. instantiating the person class, creating two person objects & calling the get_name method class Person { public $name; public $dob; public $gender; function __construct($n, $d, $g){ $this->name = $n; $this->dob = $d; $this->gender = $g; } function get_name(){ return $this->name; } } $p1 = new Person (‘Anna’, ‘11/10/83’, ‘woman’); $p2 = new Person (‘James’, ‘02/10/81’, ‘man’); echo $p1->get_name().’<br/>’; echo $p2->get_name(); Run

  11. controlling access with private, protected andpublic • php uses access modifiersto control the visibility of attributes and methods (functions) - these modifiers are placed in front of attributes and methods • the default option is public - that is, if no modifier is stated - it is assumed to be public - these can be accessed from inside or outside the class • the private access modifier can only be accessed from inside the class - if, for instance a method is a utility function and only to be used inside the class - private attributes & methods cannot be inherited • the protected access modifier means that the marked item can only be accessed from inside the class but also exists in any inherited classes - protected is kind of half way between public and private

  12. destructors • the opposite of a constructor is a destructor • this allows for some functionality to be executed just before an object is destroyed • this will happen automatically when all references to a class have been unset or fallen out of scope • a destructor for a class must be named _destruct() • a destructor cannot take parameters

  13. inheritance • inheritance allows for the building of hierarchical relationships between classes using subclasses • a subclassinheritsattributes and operations from its superclass • with inheritance it is possible to extend existing classes and thereby derive more complex and specialized classes • it also allows for common operations to be put once in the superclass rather than many times in separate subclasses

  14. new class Student inheriting from the Person class class Student extends Person { public $programme; public $number; public function __construct($n, $d, $g, $p, $no) { parent::__construct($n, $d, $g); $this->programme = $p; $this->number = $no; } function get_programme(){ return $this->programme; } function get_number(){ return $this->number; } } $s = new Student (‘Reuben’, ’25/08/93’, ‘male’, ‘Systems Analysis’, ‘12345678’); echo $s->get_name(); echo $s->get_programme(); Run

  15. abstract classes • abstract classes allow us to define base functionality that all child classes must implement • they can define concrete base functions as well as abstract functions that bindchild classes to a contract • abstract classes cannot be instantiated – e.g. you cannot have a abstract class called Person and then instantiate a new Personobject • one abstract class can be extended by another abstract class

  16. abstract class example abstract class Shape { public abstract function area(); } class Circle extends Shape { private $radius; public function __construct($r) { $this->radius = $r; } public function area() { return $this->radius * $this->radius * pi(); } }

  17. abstract class example (cont.) class Rectangle extends Shape { private $length; private $width; public function __construct($l, $w) { $this->length = $l; $this->width = $w; } public function area() { return $this->length * $this->width; } } $c = new Circle(22); echo "Area of the circle: " . $c->area() . "<br/>"; $r = new Rectangle(5, 7); echo "Area of the rectangle: " . $r->area() . "<br/>"; Run

  18. polymorphism • polymorphism is taken from the Greek and means ‘many forms’ • in oo programming it usually refers to a method (function) or operator that can have the same name but can behave differently in different contexts • for instance in the previous example the abstract class called ‘shape’ has a method called ‘area’ which is implemented in one way in the object called ‘circle’ and in another way in the object called ‘rectangle’

  19. resources Introduction to PHP Objects, Part 1 : John Coggeshall Introduction to PHP Objects, Part 2 : John Coggeshall Getting started with objects with PHP V5 : Matt Zandstra

More Related