printInfo(); $student->printInfo(); $student->studyAdvice(); ?>"> printInfo(); $student->printInfo(); $student->studyAdvice(); ?>">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

Intermediate PHP (4) Object-Oriented PHP (2). Object-oriented concepts Classes, attributes and operations Class attributes Per-class constants Class method.

Similar presentations


Presentation on theme: "Intermediate PHP (4) Object-Oriented PHP (2). Object-oriented concepts Classes, attributes and operations Class attributes Per-class constants Class method."— Presentation transcript:

1 Intermediate PHP (4) Object-Oriented PHP (2)

2 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 Key topics:

3 - implementing inheritance in php -a class can be a subclass of another class -the extends keyword specifies this use -the following code example creates a class called Academic and then a class called Student which inherits from the Academic class <?php class Academic { function printInfo () { echo "hello from the parent class ". get_class($this).' '; } function studyAdvice () { echo "You must learn some PHP "; } class Student extends Academic { function printInfo () { echo "hello from the sub-class ". get_class($this). ' '; } $teacher = new Academic; $student = new Student; $teacher->printInfo(); $student->printInfo(); $student->studyAdvice(); ?>

4 - 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

5 - example of using private member (1) <?php class Widget { private $name; private $price; private $id; public function __construct($name, $price) { $this->name = $name; $this->price = floatval($price); $this->id = uniqid(); } //checks if two widgets are the same public function equals($widget) { return(($this->name == $widget->name) AND ($this->price == $widget->price)); } ?>

6 $w1 = new Widget('Cog', 5.00); $w2 = new Widget('Cog', 5.00); $w3 = new Widget('Gear', 7.00); //TRUE if($w1->equals($w2)) { print("w1 and w2 are the same \n"); } //FALSE if($w1->equals($w3)) { print("w1 and w3 are the same \n"); } //FALSE, == includes id in comparison if($w1 == $w2) { print("w1 and w2 are the same \n"); } - example of using private member (2)

7 -private members are visible to any member of a class -private members values cannot be changed or even read outside of a method in the class -note that it is any member of class, not just a paritcular instance, may access private members -note how in the in the above example, the equals method accesses the private properties of another instance of Widget -access to private properties are made with the __get and __set methods (see last weeks lecture) - example of using private member (3)

8 - example of using protected member (1) <?php class Widget { private $name; private $price; private $id; public function __construct($name, $price) { $this->name = $name; $this->price = floatval($price); $this->id = uniqid(); } //checks if two widgets are the same public function equals($widget) { return(($this->name == $widget->name) AND ($this->price == $widget->price)); } protected function getName() { return($this->name); }

9 - example of using protected member (2) class Thing extends Widget { private $color; public function setColor($color) { $this->color = $color; } public function getColor() { return($this->color); } public function getName() { return(parent::getName()); }

10 $w1 = new Widget('Cog', 5.00); $w2 = new Thing('Cog', 5.00); $w2->setColor('Yellow'); //TRUE (still!) if($w1->equals($w2)) { print("w1 and w2 are the same \n"); } //print Cog print($w2->getName()); ?> - example of using protected member (3)

11 - Widget now has a protected method called getName -subclassing Widget to a Thing -an instance of Widget cannot call the getName method but an instance of Thing can. -another example : see printout - example of using protected member (4)

12

13 - overriding -sometimes it is useful to re-define attributes and operations (methods) -an attribute can be redefined in the subclass or a method can me made to have different functionality in the subclass – this action is called overriding <?php class A { public $myatt = ' default value'; function operation() { echo 'Parent says: '; echo 'The value of $myatt is'. $this->myatt.' '; } class B extends A { public $myatt = ' a different value'; function operation() { echo 'Child says: '; echo 'The value of $myatt is'. $this->myatt. ' '; } $a = new A; $b = new B; $a->operation(); $b->operation(); ?>


Download ppt "Intermediate PHP (4) Object-Oriented PHP (2). Object-oriented concepts Classes, attributes and operations Class attributes Per-class constants Class method."

Similar presentations


Ads by Google