Object Oriented PHP Martin Kruliš

Slides:



Advertisements
Similar presentations
ITEC200 – Week03 Inheritance and Class Hierarchies.
Advertisements

Object-Oriented PHP (1)
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
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
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
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.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
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.
Inheritance in the Java programming language J. W. Rider.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
Martin Kruliš by Martin Kruliš (v1.0)1.
Programming in Java CSCI-2220 Object Oriented Programming.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
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.
Object-Oriented Programming Chapter Chapter
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
Object Oriented Programming
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Introduction to Object-Oriented Programming Lesson 2.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
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)
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
C# for C++ Programmers 1.
Classes (Part 1) Lecture 3
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
Inheritance and Polymorphism
Object Oriented Programming in Java
Object Oriented PHP Martin Kruliš
Chapter 11 Developing Object-Oriented PHP PHP Programming with MySQL Revised by A. Philipp – Spring 2010 (Rev SP’11)
Lecture 23 Polymorphism Richard Gesick.
Java Programming Language
Lecture 22 Inheritance Richard Gesick.
Inheritance Dr. Bhargavi Goswami Department of Computer Science
More Object-Oriented Programming
PHP – Language Details Martin Kruliš by Martin Kruliš (v1.0)
Object-Oriented Programming in PHP
Fundaments of Game Design
PHP Language Revision of Fundamentals
Final and Abstract Classes
Classes and Objects Imran Rashid CTO at ManiWeber Technologies.
Lecture 10 Concepts of Programming Languages
CMPE212 – Reminders Assignment 2 due next Friday.
C++ Object Oriented 1.
Presentation transcript:

Object Oriented PHP Martin Kruliš This is an Object Oriented system. If we change something, the users object. Object Oriented PHP Martin Kruliš by Martin Kruliš (v1.1) 7. 3. 2016

Object Oriented PHP First attempt made in PHP 4 Poor design, failed Current implementation in PHP 5 Design is inspired by languages like Java or C# Adaptations for interpreted loosely-typed language E.g., there are no “virtual” methods Powerful in combination with PHP-specific features For instance with variable variables: $obj = new $className(); $obj->$methodName(); by Martin Kruliš (v1.1) 7. 3. 2016

Syntax Basics member visibility class Foo { public $var = 0; // a member variable public function bar() { // a method echo $this->var; } $instance = new Foo(); // create new instance $instance->var = 42; $instance->bar(); $instance = null; by Martin Kruliš (v1.1) 7. 3. 2016

Member Variables Member Visibility Type (is mandatory) public – accessible from anywhere protected – access. from within and derived classes private – only accessible from within the class Implicit Member Declaration Created as public by default class Foo { private $bar; } $foo = new Foo(); $foo->bar = 1; // Error! 'bar' is private $foo->barbar = 42; // OK, new member is created Note: In older versions of PHP (PHP 4 to be exact), the member variables were declared by "var" keyword. PHP 5 still recognizes var for compatibility reasons and such members are treated as public. by Martin Kruliš (v1.1) 7. 3. 2016

Member Variables Member Variables are Dynamic Why declare members in classes? Visibility control Default values (initialization) Better readability (documentation comments, …) Reflection class Point { public $x = 1; public $y = 2; } $p = new Point(); $p = new stdClass(); $p->x = 1; $p->y = 2; vs. Base class without any members (used also for implicit object constructions) For instance, when array is converted to an object, the class of that object is stdClass. by Martin Kruliš (v1.1) 7. 3. 2016

Member Functions (Methods) Visibility Type The same meaning as for variables Optional, set as public if missing Accessing the Object Instance Current object instance is available through $this It must be used to access member variables To distinguish members from local variables Otherwise the Same as Regular Functions No overloading, variadic, … by Martin Kruliš (v1.1) 7. 3. 2016

Inheritance Standard Inheritance Model Overriding Methods Each class may have only one parent class Multi-inheritance is achieved by interfaces and traits Overriding Methods All methods act as if they are virtual parent::method() – calling overridden version AncestorClass::method() – calling explicit version Methods tagged as final cannot be overridden class MyFoo extends Foo { public function Bar() { parent::Bar(); } } by Martin Kruliš (v1.1) 7. 3. 2016

Constructor Special Method __construct() Used to initialize the object Called automatically (by the new operator) May have arguments Same behavior as regular method (e.g., no overloading) Does not have to be defined Parent’s constructor or implicit constructor is used Parent’s constructor is not called implicitly Constructor should be public Except for some special cases like Singleton or Factory Method design patterns by Martin Kruliš (v1.1) 7. 3. 2016

Destructor Special Method __destruct() Called when the object is garbage-collected When ref. count reaches 0 or at the end of the script Does not have to be defined Parent’s destructor or implicit destructor is used Destructor should not throw exceptions Since they may not be handled properly Parent’s destructor is not called implicitly Destructor should be public And there are no reasonable exceptions by Martin Kruliš (v1.1) 7. 3. 2016

Member Constants Member Variables with Constant Values Declared by const prefix No visibility declaration, treated as public Accessed from class using :: operator By class name or by self identifier from within class Foo { const BAR = 42; function echoBar() { echo self::BAR; } } echo Foo::BAR; Most programmers tend to use upper case identifiers for constants (both global and class constants). by Martin Kruliš (v1.1) 7. 3. 2016

Static Members Revision Static vs. Regular (Dynamic) Members Class static variable/constant static method new operator method Object member variable logically belongs to (is stored at) is used (called) on by Martin Kruliš (v1.1) 7. 3. 2016

Static Members Static (Class) Members Declared by static keyword before the member Accessed by :: operator (like constants) E.g., MyClass::$statVar or MyClass::myFunc() One instance, no matter how many objects class has I.e., static methods does not have $this The same types of visibility as regular members Small differences in inheritance class A { static public $x; } class B extends A { static public $x; } class C extends A {} … C::$x = 42; new variable Example 1 same as A::$x by Martin Kruliš (v1.1) 7. 3. 2016

Late Static Binding Late Binding for Static Calls When static:: is used instead of self:: class A {     public static function who() { echo __CLASS__; }     public static function test() {         self::who();     } } class B extends A {     public static function who() { echo __CLASS__; } } B::test(); static::who(); Note that this is quite a unique concept as many languages strictly distinguish between early (static) binding and late (dynamic) binding. In such languages, the term “late static binding” is an oxymoron. Prints ‘A’ Prints ‘B’ by Martin Kruliš (v1.1) 7. 3. 2016

Small Mind Teaser class A { function foo() { echo (isset($this)) ? 'dynamic' : 'static'; } class B { function bar() { A::foo(); // static call A::foo(); // prints 'static' $obj = new B(); $obj->bar(); // prints 'dynamic' This is only a mental exercise. Do not call regular methods statically! by Martin Kruliš (v1.1) 7. 3. 2016

Abstract Entities Abstract Classes and Methods Prefixed with keyword abstract Abstract class cannot be instantiated Abstract method has no body It is expected to be implemented in derived class abstract class AbstractClass { abstract function foo(); } class ConcreteClass extends AbstractClass { function foo() { … foo body … } $obj = new ConcreteClass(); by Martin Kruliš (v1.1) 7. 3. 2016

Interfaces Interfaces List of public methods a class must implement Interfaces may be extended like classes Using the extends operator interface IFoo { public function bar($goo); } class Foo implements IFoo { public function bar($goo) { ... Prior to PHP 5.3.9, a class could not implement two interfaces that specified a method with the same name, since it would cause ambiguity. More recent versions of PHP allow this as long as the duplicate methods have the same signature. Unlike in case of inheritance, a class may implement multiple interfaces by Martin Kruliš (v1.1) 7. 3. 2016

Object Iterators Iterating Member Variables By foreach construct (like arrays) Keys are strings with the name of the member Only visible (accessible) members are iterated class MyClass { public $var1 = 1; public $var2 = 2; private $var3 = 3; } $obj = new MyClass(); foreach ($obj as $key => $value) { ... } Custom iteration can be implemented Interface Iterator and IteratorAggregate Both Iterator and IteratorAggregate interfaces extend abstract Traversable interface, which is empty, but it can be used to test an object, whether it has a custom iterator (by the means of instanceof operator). Example 2 by Martin Kruliš (v1.1) 7. 3. 2016

Local context is preserved Generators Detached Custom Iterators The foreach construct is powerful But it iterates over structures (arrays and objects) Custom iterator can be built (Iterator interface) Both memory demanding and tedious Generator is a function that yields values It can be used in foreach construct function range_gen($from, $to, $step) { for ($i = $from; $i < $to; $i += $step) yield $i; } foreach (range_gen(1,10,2) as $value) ... PHP 5.5 Returns next value Local context is preserved by Martin Kruliš (v1.1) 7. 3. 2016

Object Cloning Copying Reference vs. Copying Object Assignment copies reference, not the object Object copy must be invoked explicitly, by cloning $foo = new Foo(); $foo2 = $foo; $foo3 = clone $foo; Foo object #1 Note that human cloning is prohibited by law in many countries (https://en.wikipedia.org/wiki/Human_cloning). $foo $foo2 Foo object #2 $foo3 by Martin Kruliš (v1.1) 7. 3. 2016

Object Cloning Shallow vs. Full Copy Cloning process creates shallow copy by default Assignment operator is used on every member Post-cloning operations may be implemented in method __clone(), which is invoked on the copy public function __clone() { $this->innerObj = clone $this->innerObj; } $this is newly copied object, which has all members already assigned Nested cloning od inner object (a deep copy) Example 3 by Martin Kruliš (v1.1) 7. 3. 2016

Magic Methods Member Variables Accessors __get() – control read-access to members __set() – control write-access to members __isset() – isset() override for members __unset() – unset() override for members Overrides access to member variables, which are not declared or not visible Declared variables are accessed directly Only for regular members, not for static Handle with care They may lead to less readable core or even errors Implementing magic methods is sometimes called “overloading” in PHP. Do not mix-up with regular overloading that exists in other languages. There is some discussion whether using this magic assessors is actually good or bad. On one side, they may shorten your code (let you express more complex things with less writing). On the other hand, they could be slower in execution and they may create more obfuscated code (documenting magic members is tedious, there could be no autocompletion in your IDE, etc.). The following two examples show good (justifiable) application of accessors: You have an object that carries name-value pairs and you do not want the user to read the values quickly, but do not modify them (or only via methods). Mark all values private and write a __get method, which provides the access to them even from outside of the object. No __set method will result in error, when anyone wants to write the properties from the outside. You have a generic class/object, that represents e.g., a node in a complex tree structure (like XML). The accessors can be used to read or modify the structure itself (see the embedded SimleXMLElement class). Otherwise it might be better to use regular methods to achieve what you are doing/could do with magic methods. by Martin Kruliš (v1.1) 7. 3. 2016

Magic Methods Method Invocation Override Object (De)Serialization __call() – intercepts calls to not visible methods __callStatic() – the same for static methods __invoke() – when object is called as function Object (De)Serialization __sleep() – invoked when the object is being serialized (to a persistent storage) __wakeup() – invoked when the object is being deserialized (from a persistent storage) __toString() – returns string representation of the object (e.g., so it can be printed) PHP 5.4 Note that object serialization an deserialization may be done manually (see serialize() and unserialize() functions). Example 4 by Martin Kruliš (v1.1) 7. 3. 2016

Comparing Objects Reference Comparison Behavior $object1 == $object2 True if both object are of the same class and all member variables are equal (==) $object1 === $object2 True if both variables hold a reference to exactly the same object Behavior of != and !== operators can be easily extrapolated by Martin Kruliš (v1.1) 7. 3. 2016

Type Detection/Verification Operator instanceof Verifies whether object is an instance of given class or derived class, or implements given interface if ($foo instanceof FooClass) ... Functions Testing Types get_class() – returns class name as string get_parent_class() – name of the parent class is_a() – verifies that object is of given class is_subclass_of() – like is_a(), but checks also derived classes by Martin Kruliš (v1.1) 7. 3. 2016

Type Hinting Controlling Types of Function Arguments Function (method) arguments may be prefixed with Class/interface ~ the argument must be an object of that class, derived class, or it must implement the interface array keyword ~ the argument must be an array callable keyword ~ the argument must be invokeable I.e., function, or object with __invoke() method The type of the calling argument is enforced Noncompliance triggers PHP fatal error function foo(MyClass $obj, array $params) { ... } PHP 5.4 If an argument has a Class/Interface type hint and NULL as a default value, then NULL is also acceptable parameter. by Martin Kruliš (v1.1) 7. 3. 2016

Traits Traits Class-like mechanism for code reuse Trait Horizontal composition of behavior (similar to Mixins) Trait Special class that cannot be instantiated May contain both member variables and methods It can be added to regular classes trait SayHello { public function helloWorld() { echo 'Hello World'; } } class myClass { use SayHello; ... PHP 5.4 Traits have quite complex behavior, especially when combined with other OOP techniques such as inheritance or interfaces. See PHP documentation for details. by Martin Kruliš (v1.1) 7. 3. 2016

OOP-Related Functions Testing Existence class_exists(), interface_exists() method_exists() Listings get_declared_classes(), get_declared_interfaces() get_class_methods() get_object_vars() get_class_vars() Indirect Method Calls call_user_func_array(array($obj, 'methodName'), $params); by Martin Kruliš (v1.1) 7. 3. 2016

Reflection Reflection A mechanism implemented in (dynamic) languages to inspect types, objects, classes, etc. at runtime PHP holds a specific reflector class for each entity ReflectionType ReflectionFunction ReflectionClass ReflectionObject ReflectionProperty ReflectionParameter … by Martin Kruliš (v1.1) 7. 3. 2016

Reflection Reflection API in PHP An object of appropriate reflector is instantiated E.g., To list information about a class, an object of ReflectionClass is created (by the name of the class) Each reflector provides methods to access the data Class/Object reflector allows listing properties, methods, constants, and inspect inheritance Function/Method reflectors give information about arguments, modifiers, and creates dynamic closure … API also provides access to doc comments /** … */ Can be used for custom annotations Example 5 by Martin Kruliš (v1.1) 7. 3. 2016

Autoloading Automatic Loading of Classes Useful for libraries, reduces the number of includes Global function __autoload() spl_autoload_register() register multiple handlers function __autoload($className) { include 'libs/' . $className . '.php'; } log("Class $className is not defined!"); The spl_autoload_register() is preferred in case of writing libraries. It allows multiple libraries to register their own autoloading function. by Martin Kruliš (v1.1) 7. 3. 2016

Namespaces Namespaces Another way how to encapsulate space of identifiers Affect classes, traits, interfaces, functions, constants Similar to directories and files Declaration: namespace identifier; First statement in the file Identifier may be hierarchical (separator is backslash) Dereferencing myClass -> currentNS\myClass name\space\myClass -> currentNS\name\space\myClass \name\space\myClass – absolute path, no modifications Aliasing – use identifier [as identifier]; by Martin Kruliš (v1.1) 7. 3. 2016

Discussion by Martin Kruliš (v1.1) 7. 3. 2016