Object-Oriented Programming in PHP

Slides:



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

Final and Abstract Classes
Object Oriented Programming in PHP 5
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)
Object-Oriented PHP (1)
Road Map Introduction to object oriented programming. Classes
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Nikolay Kostov Telerik Corporation
UFCEUS-20-2 : Web Programming Lecture 5 : Object Oriented PHP (1)
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Chapter 8 More Object Concepts
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Martin Kruliš This is an Object Oriented system. If we change something, the users object by Martin Kruliš (v1.0)1.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Object Oriented Programming in PHP. Topics Quick OOP Review Classes Magic Methods Static Methods Inheritance Exceptions Interfaces Operators Type Hinting.
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.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Programming in Java CSCI-2220 Object Oriented Programming.
2010/11 : [1]Building Web Applications using MySQL and PHP (W1)OO PHP PHP Classes and Object Orientation.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 15 Inheritance.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Inheritance Inheritance is the process of extending the functionality of a class by defining a new class that inherit,all the features of extending class.
Classes, Interfaces and Packages
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
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.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
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.
Chapter 2 Objects and Classes
Introduction to Object-oriented Programming
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Modern Programming Tools And Techniques-I
Static data members Constructors and Destructors
Classes & Objects There are two main programming paradigms: Procedural Object-Oriented Up to now, everything we have done has been procedural.
Objects as a programming concept
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Advance OOP in PHP.
Andy Wang Object Oriented Programming in C++ COP 3330
2.7 Inheritance Types of inheritance
Final and Abstract Classes
Inheritance and Polymorphism
Table of Contents Class Objects.
Object-Oriented Programming: Classes and Objects
This pointer, Dynamic memory allocation, Constructors and Destructor
Inheritance Basics Programming with Inheritance
Introduction interface in Java is a blueprint of a class. It has static constants and abstract methods only. An interface is a way to describe what classes.
Java Programming Language
Inheritance Dr. Bhargavi Goswami Department of Computer Science
Interfaces.
Computer Programming with JAVA
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Java Inheritance.
Software Engineering for Internet Applications
CISC/CMPE320 - Prof. McLeod
Object-Oriented Programming
Chapter 14 Abstract Classes and Interfaces
Review of Previous Lesson
Review of Previous Lesson
Object-Oriented PHP (1)
Chapter 11 Inheritance and Polymorphism Part 1
Final and Abstract Classes
Classes and Objects Imran Rashid CTO at ManiWeber Technologies.
Presentation transcript:

Object-Oriented Programming in PHP

Objectives Classes and objects Methods and properties Scope Inheritance Static methods and properties Constants Abstraction and interfaces

Classes and Objects The idea of Object Oriented Programming is to move the architecture of an application closer to real world Classes are types of entities Objects are single units of a given class Example – Dog is a class, your dog Lassie is an object of class Dog Classes have methods and properties Classes and objects help to create well-structured application

Classes in PHP class Dog { … // declare methods and properties } Declaring of a class in PHP can be done anywhere in the code Two special methods: constructor and destructor Executed when creating or destroying new object of this class Used to initialize or cleanup properties and etc. class Dog { … // declare methods and properties }

Classes in PHP Method name and body class name Class definition begins with the class keyword, followed by its name and methods and properties list Objects of class (instances) are created with the keyword new Method name and body class name class A { function foo () { echo "foo here!"; } } $myFirstObject = new A(); $myFirstObject->foo(); // prints out "foo here!"; Create new object of class A Execute method of this object

Constructors class A { function __construct ($bar) { echo $bar; } Each class can have only one constructor All parameters of the creating of the object are passed to the constructor class A { function __construct ($bar) { echo $bar; } function foo () { echo "foo here!"; } } $myFirstObject = new A('test'); // print 'test'

Properties Class can have unlimited number of properties The $this variable points to the current object – called execution context class A { var $bar; function __construct ($arg) { $this->bar = $arg; } function myPrint () { echo $this->bar; } $myFirstObject = new A('test'); $myFirstObject->myPrint();

More Properties class A { var $bar = 'default value'; … class A { Class can specify default value for a property Properties can be accessed from the outside world class A { var $bar = 'default value'; … } $obj = new A; echo $obj->bar;

$this Example of what $this is class A { var $bar; Can be used to access methods too $this class A { var $bar; function __construct ($bar) { $this->bar = $bar; } function myPrint () { echo $this->bar; } $myFirstObject = new A('test'); $myFirstObject->myPrint(); // prints 'test' $anotherObject = new A('foo'); $anotherObject ->myPrint(); // prints 'foo';

Destructors Each class can have only one destructor class A { Must be public Destructors are automatically called when script is shutting down class A { function __construct ($name) { $this->fp = fopen ($name, 'r'); } function __destruct () { fclose($this->fp); } $myFirstObject = new A('test');

Scope Each method and property has a scope It defines who can access it Three levels – public, protected, private Private can be access only by the object itself Protected can be accessed by descendant classes (see inheritance) Public can be accessed from the outside world Level is added before function keyword or instead of var var is old style (PHP 4) equivalent to public Constructors always need to be public

Scope Example class A { private $bar; public function __construct ($bar) { $this->bar = $bar; } public function myPrint () { echo $this->bar; } $myFirstObject = new A('test'); $myFirstObject->myPrint(); // prints 'test' // this will not work: echo $myFirstObject->bar; The $bar variable is private so only the object can access it The myPrint method is public, so everyone can call it

Inheritance A class can inherit (extend) another class class A { It inherits all its methods and properties class A { public $bar = 'test'; public function example () { … } } class B extends A { … } $obj = new B(); echo $obj->bar; //prints 'test' //calls the A-class function $obj->example();

Protected Scope Method or property, declared as protected can be accessed in classes that inherit it, but cannot be accessed from the outside world class A { protected $bar = 'test'; } class B extends A { public function foo () { // this is allowed $this->bar = 'I see it'; } } $obj = new B(); echo $obj->bar; //not allowed

Overriding class A { public foo() { … } } class B extends A { When a class inherits another, it can declare methods that override parent class methods Method names are the same Parameters may differ class A { public foo() { … } } class B extends A {

Overriding Example class A { public foo() { echo 'called from A'; } class B extends A { echo 'called from B'; $obj1 = new A(); $obj2 = new B(); $obj1->foo(); // executes A's methods $obj2->foo(); // executes B's methods

Accessing Parent Class As -> is used to access object's methods and properties, the :: (double colon) is used to change scope Scope Resolution Operator parent:: can be used to access parent's class overridden methods Example: call parent's constructor in the child one

Accessing Parent Class Example of calling parent constructor class A { protected $variable; public __construct() { $this->variable = 'test'; } class B extends A { parent::__construct(); echo $this->variable; $obj1 = new B(); // prints 'test';

The static Keyword Defining method or property as 'static' makes them accessible without needing an instantiation of a class Accessed with the double-colon (::) operator instead of the member (->) operator $this is not available in static methods Static properties and methods can also have scope defined – public, private or protected

The static Keyword Example of static method and property class A { Class can access statics with the self keyword Outside world accesses statics with the class name class A { public static $myVariable; public static function myPrint() { echo self::$myVariable; } } A::$myVariable = 'test'; A::myPrint();

Class Constants Constants in PHP usually are declared with the define function Constants can be defined in class Differ from normal variables – no need for $ symbol to declare and access Declared with the const keyword Value must be supplied with the declaration Accessed with scope operator (::) Can be overridden by child classes Value must be constant expression, not a variable, class member, result of operation or function call

Class Constants class A { const myConstant = 'value'; public function showConstant() { echo self::myConstant; } } echo A::myConstant; $obj = new A(); $obj->showConstant(); Example of a class constant

Abstraction Classes, defined as abstract, cannot have instances (cannot create object of this class) Abstract class must have at least one abstract method Abstract methods do not have implementation (body) in the class Only signature The class must be inherited The child class must implement all abstract methods Cannot increase visibility

Abstraction Example abstract class AbstractClass { abstract protected function getValue(); abstract public function getValue2($prefix); public function printOut () { echo $this->getValue(); } } class Class1 extends AbstractClass { protected function getValue (){ return "Class1"; } public function getValue2($prefix) { return $prefix."NAC1"; }

Abstraction Example (2) // continue from previous slide class Class2 extends AbstractClass { protected function getValue (){ return "Class2"; } public function getValue2($prefix) { return $prefix."NAC2"; } $class1 = new Class1(); $class1->printOut(); // "Class1"; echo $class1->getValue2('FOO'); // FOONAC1 $class2 = new Class2(); $class2->printOut(); // "Class2"; echo $class2->getValue2('FOO'); //FOONAC2

Interfaces Object interfaces allow you to specify what methods a child class must implement Declared with the interface keyword Similar to abstract class Interface can have only public methods No method in interface can have implementation Interfaces are inherited with the implements keyword (instead of extends) One class may implement multiple interfaces, if they do not have methods with same names

Interface Example interface iTemplate { public function set ($name, $value); public function getHTML($template); } class Template implements iTemplate { private $vars = array(); public function set ($name, $value) { $this->vars[$name] = $value; } public function getHTML($ str) {  foreach($this->vars as $name=>$value) { $ str = str_replace( $name, $value, $str);         }     return $template; } }

Quiz Question #1- Question #2- Describe the difference between echo and Print statements with examples Question #2- Write a Program in which a function will be created at run time, calculate an employee current age and years employed by getting two arguments(dob and date-of-joining). Also display the name of the run time created function.