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.

Slides:



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

OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Java Programming 2 Dr. Priti Srinivas Sajja Introductory concepts of java programming as specified in PGDCA 203:Object Technology, S P University.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
OOP: Inheritance By: Lamiaa Said.
Inheritance Inheritance Reserved word protected Reserved word super
Object-Oriented PHP (1)
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 3rd Edition.
16/22/2015 2:54 PM6/22/2015 2:54 PM6/22/2015 2:54 PMObject-Oriented Development Concept originated with simulating objects and their interactions. Adapted.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
UFCEUS-20-2 : Web Programming Lecture 5 : Object Oriented PHP (1)
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Intro to OOP with Java, C. Thomas Wu
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
1 Chapter 10: Data Abstraction and Object Orientation Aaron Bloomfield CS 415 Fall 2005.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
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.
OOP with PHP Roman Bednarik
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.
28-Dec-04polymorhism.ppt1 Polymorphism. 28-Dec-04polymorhism.ppt2 signatures in any programming language, a signature is what distinguishes one function.
Inheritance Chapter 10 Programs built from objects/instances of classes An O.O. approach – build on earlier work. Use classes in library and ones you have.
 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.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) * Acknowledgement:
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Java Class Structure. Class Structure package declaration import statements class declaration class (static) variable declarations* instance variable.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Coming up: Inheritance
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.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
OOP Basics Classes & Methods (c) IDMS/SQL News
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
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.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Lecture 12 Inheritance.
Inheritance and Polymorphism
Agenda Warmup AP Exam Review: Litvin A2
12 Data abstraction Packages and encapsulation
Interfaces.
Java Inheritance.
Object-Oriented Programming in PHP
Fundaments of Game Design
Object-Oriented Programming
Chapter 14 Abstract Classes and Interfaces
CIS 199 Final Review.
Object-Oriented PHP (1)
Final and Abstract Classes
Classes and Objects Imran Rashid CTO at ManiWeber Technologies.
Chengyu Sun California State University, Los Angeles
Presentation transcript:

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 using the keyword var  Can be initialized when declared To create an instance of a class, use new followed by the class name  Returns a reference to the instance Use the selector -> to access a property of an instance  The property name here does not begin with $  For variable interpolation in a double-quoted string, box the selector expression within {…}s

class Simple1 { var $x = 1, $y = 2; } $s = new Simple1; echo " {$s->x} "; echo " {$s->y} "; Outputs 1 2

Methods are defined as normal functions within the class definition A method is invoked by an instance using the selector ->, e.g., $s->sum(6) Pseudo-variable $this is used in method definitions to reference the calling object (the current instance)

Several functions whose names begin with double underscore are “magical” in PHP classes  Can’t have functions with these names in a classes unless you want the magic functionality associated with them __construct(), if defined, is called when the class is instantiated to allow instances to be initialized  Commonly pass initialization parameters __destruct(), if defined, is called when the object becomes eligible for garbage collection __toString() returns the string that provides the value of an instance when it appears where a string is expected (conversion to string)

class Simple2 { var $x, $y; function __construct($first, $second) { $this->x = $first; $this->y = $second; } function __toString() { return strval($this->x); } function sum( $val ) { return $this->x + $this->y + $val; } } Continued

$s = new Simple2(2, 4); echo " {$s} "; echo " {$s->y} "; echo " {$s->sum(6)} "; Outputs

Access Modifiers Access modifiers control the visibility of properties and methods  Placed in front of property and method declarations public (the default): accessible from inside or outside the class private : accessible only from inside the class protected : wait until we get to inheritance

Accessor Functions The values of private properties of instances can be accessed if we define the magical method __get() A minimal implementation is function __get($prop) { return $this->$prop; // Note the '$' on '$prop' }

class Simple3 { private $x, $y; public function __construct($first, $second) { $this->x = $first; $this->y = $second; } public function __toString() { return strval($this->x); } public function __get($prop) { return $this->$prop; } } Continued

$s = new Simple3(2, 4); echo " {$s} "; echo " {$s->y} "; Outputs 2 4 When $s->y is executed, __get() is called and passed y The body of __get() can modify the value before it is returned— e.g., return abs($this->$prop);

The value of a private property can be updated if we define the magical method __set() A minimal implementation is function __set($prop, $val) { $this->$prop = $val; } Suppose this is inserted in the above code, and we add $s = new Simple3(2, 4); $s->y += 10; echo " {$s->y} ";  Outputs 14 When $s->y += 10; is execute, __set() is called and passed y and 10

The body of __set() could change the value that is written to the property—e.g., if ( $val > 100 ) $val = 100; $this->$prop = $val;

Inheritance Use the extends keyword to indicate that a class is a subclass of another Properties and methods are inherited unless they’re overridden To invoke a method of the parent that’s overridden in the current class, precede the method name with parent:: —e.g., parent::foo(2)  The parent’s constructor is not automatically called—must use parent::__construct(…);  The same applies to destructors Access modifier protected makes the property or method invisible outside the class  But (unlike private ) allows it to be inherited

class A { protected $x; protected $y = 5; function __construct($a) { $this->x = $a; } public function foo() { return $this->x + $this->y; } public function bar() { return $this->x - $this->y; } } Continued

class B extends A { protected $y = 10; public function __construct($a) { parent::__construct($a); } public function bar() { return $this->x * $this->y; } public function xan() { return $this->x / $this->y; } } Continued

$c1 = new A(1); echo " {$c1->foo()} "; echo " {$c1->bar()} "; $c2 = new B(2); echo " {$c2->foo()} "; echo " {$c2->bar()} "; echo " {$c2->xan()} "; Outputs

Preventing Inheritance and Overriding with final If a function declaration is preceded by keyword final, it can’t be overridden in subclasses Preceding a class definition with final prevents it from being subclassed

Abstract Methods and Classes Declare an abstract method by preceding its signature (with no implementation) with keyword abstract Declare an abstract class by preceding its definition with keyword abstract Can’t create an instance of an abstract class Any class that contains at least 1 abstract method must also be abstract When inheriting from an abstract class, all abstract methods of the parent must be defined by the child  These methods must be defined with the same or a less restricted visibility  E.g., if the abstract method is defined as protected, the implementation must be protected or public, but not private

abstract class A { protected $x; protected $y; public function foo() { return $this->x + $this->y; } abstract protected function bar(); }

class B extends A { public function __construct($a, $b) { $this->x = $a; $this->y = $b; } public function bar() { return $this->x - $this->y; } } $c1 = new B(1, 2); echo " {$c1->foo()} "; echo " {$c1->bar()} "; Outputs 3

Implementing Interfaces Interfaces are seen as workarounds for multiple inheritance (PHP allows only single inheritance) They’re similar to interfaces supported by other OO languages (e.g., Java) An interface specifies a set of functions (giving just their signatures) that must be implemented in classes that implement the interface A class can inherit from one class and implement 1 or more interfaces

interface A { public function foo($a); } class B { private $x=10; public function bar() { return $this->x; } } Continued

class C extends B implements A { public function foo($a) { return $a; } } $c1 = new C; echo " {$c1->foo(8)} "; echo " {$c1->bar()} "; Outputs 8 10

Usefulness for Generating HTML File cl8a.inc <?php class A { public function greeting() { echo " Hello, user! "; } public function intro() { echo " This is an experimental website. "; } } ?>

File cl8b.inc <?php class B extends A { public function intro() { echo " This is a student website. "; } public function signoff() { echo " Good bye! "; } } ?>

File cl8.php <?php include('cl8a.inc'); include('cl8b.inc'); $page = new B; $page->greeting(); $page->intro(); $page->signoff(); ?>