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

Slides:



Advertisements
Similar presentations
PHP functions What are Functions? A function structure:
Advertisements

Introduction to classes Sangeetha Parthasarathy 06/11/2001.
Final and Abstract Classes
Python Objects and Classes
CS 211 Inheritance AAA.
Lecture 28: Abstract Classes & Inheritance Announcements & Review Lab 8 Due Thursday Image and color effects with 2D arrays Read: –Chapter 9 Cahoon & Davidson.
Class Hierarchies. Inheritance is the capability of a class to use the properties and methods of another class while adding its own functionality. An.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
OBJECT-ORIENTED PROGRAMMING. What is an “object”? Abstract entity that contains data and actions Attributes (characteristics) and methods (functions)
Chapter 10: Introduction to Inheritance
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
More about classes and objects Classes in Visual Basic.NET.
Object-Oriented PHP (1)
Introduction to Data Structures, Spring 2007 Slide- 1 California State University, Fresno Introduction to Data Structure Object Oriented Programming Concepts.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
UFCEUS-20-2 : Web Programming Lecture 5 : Object Oriented PHP (1)
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
UFCEWT-20-3 : Advanced Topics in Web Development Lecture 7 : Object Oriented PHP (2)
Object Oriented Programming in PHP. Topics Quick OOP Review Classes Magic Methods Static Methods Inheritance Exceptions Interfaces Operators Type Hinting.
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
OOP IN PHP `Object Oriented Programming in any language is the use of objects to represent functional parts of an application and real life entities. For.
Programming using C# for Teachers Introduction to Objects Reference Types Functions of Classes Attributes and Types to a class LECTURE 2.
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved2/23 Outline Creating Subclasses Overriding Methods Class Hierarchies.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Object-Oriented Programming Chapter Chapter
1 CS 177 Week 11 Recitation Slides Class Design/Custom Classes.
1 The finalize, clone, and getClass Methods  The finalize method is invoked by the garbage collector on an object when the object becomes garbage.  The.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
ISBN Object-Oriented Programming Chapter Chapter
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Inheritance 2 Mehdi Einali Advanced Programming in Java 1.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
CS1101 Group1 Discussion 6 Lek Hsiang Hui comp.nus.edu.sg
Basic Concepts of OOP.  Object-Oriented Programming (OOP) is a type of programming added to php5 that makes building complex, modular and reusable web.
21. PHP Classes To define a class, use the keyword class followed by the name and a block with the properties and method definitions Properties are declared.
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Object-Oriented PHP (2) Intermediate OO Concepts & Practice (1)
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
UNIT-IV WT. Why use classes and objects? PHP is a primarily procedural language small programs are easily written without adding any classes or objects.
Object-oriented programming (OOP) is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their.
Objects as a programming concept
Advanced Programming in Java
CS240: Advanced Programming Concepts
Java Inheritance.
Java Programming Language
Advanced Programming Behnam Hatami Fall 2017.
Lecture 14- Abstract Classes
Advanced Java Programming
Java Programming, Second Edition
Object-Oriented Programming in PHP
CISC124 Assignment 3 sample solution will be posted tonight after 7pm.
Chapter 9 Carrano Chapter 10 Small Java
Object-Oriented Programming
Chapter 14 Abstract Classes and Interfaces
Chapter 11 Inheritance and Polymorphism
Review of Previous Lesson
Object-Oriented PHP (1)
Final and Abstract Classes
Classes and Objects Imran Rashid CTO at ManiWeber Technologies.
Winter 2019 CMPE212 5/25/2019 CMPE212 – Reminders
Presentation transcript:

Intermediate PHP (4) Object-Oriented PHP (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:

- 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(); ?>

- 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

- 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)); } ?>

$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)

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

- 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); }

- 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()); }

$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)

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

- 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(); ?>