Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object-Oriented PHP (1)

Similar presentations


Presentation on theme: "Object-Oriented PHP (1)"— Presentation transcript:

1 Object-Oriented PHP (1)

2 Key topics: Object-oriented concepts
Classes, attributes and operations Class attributes Per-class constants Class method invocation Inheritance Access modifiers Static methods Type hinting Late static bindings Object cloning Abstract classes Class design Implementing design Advanced OO functionality

3 - understanding Object-oriented concepts
many modern programming languages either require (java, ruby, smalltalk etc.) or support (php, perl, F# etc.) the o-o 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.

4 - classes and objects (1)
in o-o terminology, an “object” can represent something “real” (e.g. an user) or something “conceptual” (e.g. a file) o-o 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 alter the internals of an object to improve performance, add new features etc. without altering its interface hence, an o-o 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 - polymorphism polymorphism is taken from the Greek and means ‘many forms’ in o-o 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 an abstract class called ‘shape’ might have a method called ‘area’ which will be implemented in one way in an object called ‘circle’ and in another way in an object called ‘square’

7 - inheritance inheritance allows for the building of hierarchical relationships between classes using subclasses a subclass inherits attributes and operations from its superclass with inheritance it is possible to extend existing classes and thereby derive more complex and specialised classes it also allows for common operations to be put once in the superclass rather than many times in separate subclasses

8 - creating classes, attributes & operations in PHP
structure of a class a minimal class definition class classname { } however, to be useful a class needs attributes and operations attributes are created using keywords that match their visibility : public, private or protected the following code creates a class with two public attributes class person { public $name; public $age; }

9 operations or methods are created by declaring functions within the class definition
the following code creates a class named classname that do nothing the first operation op1() takes no parameters and the second op2() takes two parameters class classname { function op1() { } function op2 ($param1, $param2){

10 - 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() the following code defines a class with a constructor class classname { function _construct($param) { echo “Constructor called with parameter ”.$param.“<br/>”; }

11 - destructors the opposite of a constructor is a destructor
this allows for some functionality to be executed just before an object is destryed 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

12 - 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 creates a class called classname with a constructor that expects one parameter - then three objects of that class are created

13 class classname { function _construct($param) { echo “conctruct called with”.$param.“<br/>”; } $a = new classname(“First”); $b = new classname(“Second”); $c = new classname(); what will be the output?

14 - using class attributes (1)
within a class, attributes can be accessed using the special pointer $this for instance an attribute called $attribute can be accessed with $this—>attribute the following code illustrates setting and accessing an attribute within a class class classname { public $attribute; function operation($param) { $this—>attribute = $param; echo $this—>attribute; }

15 - using class attributes (2)
weather an attribute can be accessed from outside a class is determined by access modifiers however, it is generally not recommended that attributes are accessed directly from outside the class - this is because the o-o approach encourages data hiding or encapsulation this is done using the __get & __set access functions; e.g. class classname { public $attribute; function __get($name) { return $this—>name; } function __set($name, $value) { $this—>name = value;

16 - using class attributes (3)
the previous code example shows two interfaces to the $attribute attribute the __get() function simple returns the value of $attribute and __set() function assigns a new value to $attribute note that __get() takes one parameter - the name of an attribute and the __set() function takes two parameters - the name of an attribute and the value it is to be set to also note the use of the double underscore as in the __construct() and __destruct() functions php gives these functions special meaning and the __get() and __set() functions can be now be used to get and set the value of any attribute, e.g. $a = new classname; $a—>attribute = 5; // this will implicitly call the // set function on the attribute // called $attribute

17 - why bother? with a single access point (interface) it is now easy to implement validity checks for instance, if we now wanted to ensure that $attribute always has to be between 1 and we only need to modify the code in one place function __set($name, $value) { if (($name==“attribute” && ($value >= 1) && ($value <= 100) $this—>attribute = $value; }

18 - controlling access with private and public
php uses access modifiers to 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


Download ppt "Object-Oriented PHP (1)"

Similar presentations


Ads by Google