Object Oriented Programming in PHP. Topics Quick OOP Review Classes Magic Methods Static Methods Inheritance Exceptions Interfaces Operators Type Hinting.

Slides:



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

OOP Abstraction Classes Class Members: Properties & Methods Instance (object) Encapsulation Interfaces Inheritance Composition Polymorphism Using Inheritance.
Object-Oriented Programming Python. OO Paradigm - Review Three Characteristics of OO Languages –Inheritance It isn’t necessary to build every class from.
Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar Fundamentals of Web DevelopmentRandy.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Object-Oriented PHP (1)
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Nikolay Kostov Telerik Corporation
Introducing PHP Data Objects Wez Furlong
Multiple Choice Solutions True/False a c b e d   T F.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Object Orientation An Object oriented approach views systems and programs as a collection of interacting objects. An object is a thing in a computer system.
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.
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
APCS Java AB 2004 Review of CS1 and CS2 Review for AP test #1 Sources: 2003 Workshop notes from Chris Nevison (Colgate University) AP Study Guide to go.
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.
OOP and Dynamic Method Binding Chapter 9. Object Oriented Programming Skipping most of this chapter Focus on 9.4, Dynamic method binding – Polymorphism.
Object Oriented Programming in PHP. List of Items of Interest What is a Class What is an Object What is a Singleton Multiple Objects, How to store. Advantages.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Programming in Java CSCI-2220 Object Oriented Programming.
Date : 02/03/2014 Web Technology Solutions Class: OOP PHP, Design Patterns and CRUD.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Class Builder Tutorial Presented By- Amit Singh & Sylendra Prasad.
Object-Oriented Programming Chapter Chapter
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
An Advanced Code Pattern: Inner Classes CSE301 University of Sunderland Harry R. Erwin, PhD Half Lecture.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Polymorphism, Virtual Methods and Interfaces Version 1.1.
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.
Intermediate PHP (4) Object-Oriented PHP (2). Object-oriented concepts Classes, attributes and operations Class attributes Per-class constants Class method.
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.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
OOP Basics Classes & Methods (c) IDMS/SQL News
C# Fundamentals An Introduction. Before we begin How to get started writing C# – Quick tour of the dev. Environment – The current C# version is 5.0 –
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
OOP, Inheritance and Polymorphism Lecture 6. Object relations  Inheritance is ‘a kind of’, ‘a type of’ e.g. a revolver is a type of gun  Aggregation.
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.
Introduction to Object-oriented Programming
Objects as a programming concept
Inheritance ITI1121 Nour El Kadri.
Web Technology Solutions
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
Agenda Warmup AP Exam Review: Litvin A2
Chapter 3: Using Methods, Classes, and Objects
Java Programming Language
Inheritance Dr. Bhargavi Goswami Department of Computer Science
Object Oriented Programming
User-Defined Classes and ADTs
Object-Oriented Programming in PHP
Chapter 8 Classes User-Defined Classes and ADTs
Review of Previous Lesson
Object-Oriented PHP (1)
Final and Abstract Classes
Classes and Objects Imran Rashid CTO at ManiWeber Technologies.
Object Oriented PHP.
C++ Object Oriented 1.
Presentation transcript:

Object Oriented Programming in PHP

Topics Quick OOP Review Classes Magic Methods Static Methods Inheritance Exceptions Interfaces Operators Type Hinting Using PDO

What is an Object? Encapsulated data with logic An object has properties (data) and methods (logic)

Not everything is an object in PHP But all objects have (a) class

What is a Class? A way of saying “all objects of this type share these attributes”

All tables have legs and a surface Our world has many objects (chairs, exams, students, etc.) but we can recognize tables because:

All Users have a username, a password and can authenticate() There are many kinds of objects in our code but the computer can recognize Users because…

Class Syntax Class Example { public $foo; const $baz = 42; public function bar() { // put code here }

Understanding Visibility public – visible to everyone protected – visible only to classes which are derived from this class private – visible only to this class default (nothing listed) – means public; only for methods

Magic Methods PHP will call some specially named methods to enable a class to perform specific operations. They all start with __ Never let any of your code start with __

__construct() Constructor Called when a new instance is created

__toString() How to convert the object to a string Used whenever you use your object with the print or. (concatenate) operators.

Class Constants Constants cannot change They exist at the CLASS level not in instances. Class Foo { const bar = 42; } Print Foo::bar;

Static Properties and Methods Static members are CLASS level not instance level class Foo { public static $bar = “bar”; public static function baz() {} }

self Used to access static members of the current class (uses the most derived class) self::$bar

New Operators new -> ::

new operator Used to instantiate a class. $foo = new Foo();

-> operator Used to access a property or method of an instance. $foo->bar(); $foo->baz = 2;

:: operator Like the -> but works on the level of CLASSES not instances. How you access static and constant members Class Foo { const bar = 42; } Print Foo::bar;

Inheritance A way of sharing common behaviour among classes PHP is a single inheritance language A class may have no parent or 1 parent only.

Inheritance Syntax class Bar {} class Foo extends Bar {}

Accessing the parent class Foo extends Bar { public function __construct() { parent::__construct(); parent::$foo; }

abstract class An abstract class is a class you cannot instantiate

Correct abstract class Foo { public $foo; }

Correct abstract class Foo { abstract public bar(); }

Incorrect class Foo { abstract public bar(); }

Interfaces An interface is like an abstract class where all the methods are abstract. An interface may not have any properties. However, a class may IMPLEMENT multiple interfaces. May not have private members.

Interface Syntax interface Fooable { public function foo(); protected function bar(); } class Foo implements Fooable { public function foo() {} protected function bar() [}; }

Passing Arguments by Reference or Copy

Passing By Copy When a scalar value is passed to a function it is passed “by copy”. Changes to the value inside the function do not affect the value “outside”, or after, the function or call.

Passing by Reference When an object is passed to a function it is passed “by reference”. Changes to the value inside the function do affect the value “outside”, or after, the function or call.

Passed by Reference vs. Copy Passed By ReferencePassed By Copy Object class Foo { private $f; } $foo = new Foo(); foobar($foo); Scalar string $s = “string”; foobar($s); Number $n = 1; foobar($n); Array $a = [1, 1, 2, 3]; foobar($a);

Type Hinting Type hinting lets us specify the type of a parameter.

Hintable Types Can SpecifyCannot Specify array function foo(array $foo) {} Class Name function foo(Exception $e) {} Interface Name function foo(Nameable $n) {} String Number Scalar Trait

New Key Words and Operator $this self:: parent:: -> :: new static class interface public private protected

PDO PHP Data Objects

PDO A common API for doing database operations with any database. Supports most databases with the same classes/functions

Create a Connection $dbh = new PDO( 'mysql:host=localhost;dbname=test', $user, $pass);

Create a Statement $stmt = $dbh->prepare( "INSERT INTO table(col1, col2) VALUES (?, ?)");

Execute the statement $stmt->execute(array(1, 2)) $stmt->execute()

Fetch Results while ($row = $stmt->fetch()) {$stmt->fetch() print_r($row); } foreach ( $stmt->fetchAll() as $row) { print_r($row) }$stmt->fetchAll()

PDO lifecycle Create Handle Prepare Statement Execute Statement Fetch