Object Oriented Programming in PHP 5

Slides:



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

Final and Abstract Classes
Pemrograman Web Object Oriented Programming in PHP 5.
Python Objects and Classes
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
Advanced JS The World's Most Misunderstood Programming Language ) Douglas Crockford( Shimon Dahan
Object-Oriented PHP (1)
Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
ASP.NET Programming with C# and SQL Server First Edition
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.
PHP Workshop ‹#› PHP Classes and Object Orientation.
UFCEUS-20-2 : Web Programming Lecture 5 : Object Oriented PHP (1)
Chapter 8 More Object Concepts
1 Understanding Inheritance COSC 156 C++ Programming Lecture 8.
Programming Pillars Introduction to Object- Oriented Programming.
Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.
ClassesPHPMay-2007 : [‹#›] PHP Classes and Object Orientation.
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.
1 SystemVerilog Enhancement Requests Daniel Schostak Principal Engineer February 26 th 2010.
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.
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.
Abstract Classes. Review PA – 3 Design Considerations /Web/CS239/programs/pa3/draft.php ?harrisnlCS239
2010/11 : [1]Building Web Applications using MySQL and PHP (W1)OO PHP PHP Classes and Object Orientation.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
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.
C++ Inheritance Data Structures & OO Development I 1 Computer Science Dept Va Tech June 2007 © McQuain Generalization versus Abstraction Abstraction:simplify.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
CITA 342 Section 1 Object Oriented Programming (OOP)
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Basic Concepts of OOP.  Object-Oriented Programming (OOP) is a type of programming added to php5 that makes building complex, modular and reusable web.
Classes in PHP Web Engineering. What is Class? A class is a collection of variables and functions working with these variables. Variables are defined.
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(Objects& Class) Classes are an expanded concept of data structures: like.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Chapter 2 11/18/2015 © by Pearson Education, Inc. All Rights Reserved. Lect9 GC 2011.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Week 5 Extending classes in Java. Extending classes u Using an existing classes as the basis for a new one u Defining only the differences between the.
Object Lifetimes and Dynamic Objects Lecture-7. Object Lifetimes and Dynamic Objects External (Global) Objects Persistent (in existence) throughout the.
Lecture 12 Inheritance.
Programming with ANSI C ++
Final and Abstract Classes
Chapter 5 Classes.
Road Map Introduction to object oriented programming. Classes
Chapter 11 Developing Object-Oriented PHP PHP Programming with MySQL Revised by A. Philipp – Spring 2010 (Rev SP’11)
Nested class.
Understanding Inheritance
Object-Oriented Programming: Inheritance
Web Systems Development (CSC-215)
Inheritance Constructors & Overriden Functions
The super Reference Constructors cannot be used in child classes, even though they have public visibility Yet we often want to use the parent's constructor.
PHP Classes and Object Orientation
Interface.
Interfaces.
Java – Inheritance.
Object-Oriented Programming in PHP
Object-Oriented Programming: Inheritance
Inheritance.
Object-Oriented PHP (1)
Final and Abstract Classes
Classes and Objects Imran Rashid CTO at ManiWeber Technologies.
Presentation transcript:

Object Oriented Programming in PHP 5 Pemrograman Web Object Oriented Programming in PHP 5

Action or Method What is a function? Conceptually, what does a function represent? …give the function something (arguments), it does something with them, and then returns a result… Action or Method

What is a class? Conceptually, a class represents an object, with associated methods and variables Say we’ll talk more about the application of OBJECTS and object orientated programming later, for now we are going to learn in practice how to use the things..

Class Definition Example <?php // filename: manusia.class.php class manusia { public $nama; public function menyapa() { echo 'Halo!'; } } ?>

Class Defintion Similar to defining a function.. The definition does not do anything by itself. It is a blueprint, or description, of an object. To do something, you need to use the class…

Class Usage <?php require('manusia.class.php'); $susan = new manusia; $susan->nama = 'Susan'; echo $susan->nama . ' jika menyapa, berkata: ' . $susan->menyapa(); ?>

Using attributes within the class.. If you need to use the class variables within any class actions/methods, use the special variable $this in the definition: class manusia { public $nama; public function menyapa() { echo $this->nama . ' bilang Halo!'; } } $susan = new manusia; $susan->nama = 'Susan'; $susan->menyapa();

Constructor methods A constructor method is a function that is automatically executed when the class is first instantiated. Create a constructor by including a function within the class definition with the __construct name. Remember.. if the constructor requires arguments, they must be passed when it is instantiated!

Constructor Example <?php class manusia { public $nama; public function __construct($nama) { $this->nama = $nama; } public function menyapa() { echo $this->nama . ' bilang Halo!'; } ?>

Constructor Example <?php … $susan = new manusia('Susan'); $susan->menyapa(); ?> Output: Susan bilang Halo!

Class Scope Like functions, each instantiated object has its own local scope. <?php $mahasiswi = new manusia('Susan'); $mahasiswa = new manusia('Joko'); echo $mahasiswa->nama; // Joko echo $mahasiswi->nama; // Susan; ?>

Inheritance manusia parent children mahasiswa dosen The real power of using classes is the property of inheritance – creating a hierarchy of interlinked classes. manusia parent children mahasiswa dosen

Inheritance The child classes 'inherit' all the methods and variables of the parent class, and can add extra ones of their own. e.g. the child classes mahasiswa inherits the variable nama and method menyapa from the manusia class, and can add extra ones…

Inheritance example <?php class mahasiswa extends manusia { public function __construct($nama){ $this->nama = $nama; } public $tugas = 'belajar'; $susan = new mahasiswa('Susan'); echo $susan->menyapa() . ' ketika sedang ' . $susan->tugas; Output: Susan bilang Halo! ketika sedang belajar

Method Override Mahasiswa selalu berkata 'Hei!' ketika menyapa. <?php class mahasiswa extends manusia { … public function menyapa(){ echo $this->nama . ' bilang Hei!'; } $susan = new mahasiswa('Susan'); echo $susan->menyapa(); Output: Susan bilang Hei! ketika sedang belajar

Child Constructors? If the child class possesses a constructor function, it is executed and any parent constructor is ignored. If the child class does not have a constructor, the parent's constructor is executed. If the child and parent does not have a constructor, the grandparent constructor is attempted… … etc.

Objects within Objects It is perfectly possible to include objects within another object <?php class pakaian { public $warna = 'merah'; } class manusia { public $nama; public $baju; public function __construct( $nama ) { $this->nama = $nama;

Objects within objects example <?php $susan = new manusia('Susan'); $susan->baju = new pakaian; echo $susan->nama . ' memakai baju warna ' . $susan->baju->warna; ?> Output: Susan memakai baju warna merah

Encapsulation Encapsulation is a way of storing an object or data as a property within another object, so that the outer object has full control over what how the internal data or object can be accessed. This, in combination with making the inner object/property private, enables information hiding.

Encapsulation Example <?php class pakaian { public $warna = 'merah'; } class manusia { private $baju; public function __construct() { $this->baju = new pakaian; $this->baju->warna = 'biru'; public function warnaBaju() { return $this->baju->warna; $susan = new manusia(); echo 'Susan memakai baju berwarna ' . $susan->warnaBaju(); Output: Susan memakai baju berwarna biru

Abstract Class It's a kind "father" that must be inherited to be used. Classes that inherit differ from them only in the abstract methods and can access the methods of the parent class using the keyword parent. Features: can not be instantiated methods can be abstract (not implemented) methods may be not abstract (implemented) a class can inherit from a single abstract class

Interface The clearest definition is that an interface is a contract. Features: All classes that implement an interface must develop all the methods that have been defined The class implementing the interface must use the exact same method signatures as are defined in the interface. Not doing so will result in a fatal error All methods declared in an interface must be public, this is the nature of an interface A class can implement more than one interface An interface can be used by the Type Hinting

Polymorphism Polymorphism is the ability (in programming) to present the same interface for differing underlying forms (data types).

Polymorphism Example <?php interface class binatang { public function bicara(); } class kucing implements binatang { public function bicara() { echo "Meong…"; class anjing implements binatang { echo "Guk… Guk…";

Polymorphism Example <?php $hewan = new kucing; $hewan->bicara(); // Meong... $hewan = new anjing; $hewan->bicara(); // Guk... Guk... ?>

Final Keyword PHP introduces "Final" keyword to prevent sub-class method overriding. "Final" keyword can be implemented on properties, methods and classes Final class means it cannot be inherited

Deleting objects So far our objects have not been destroyed till the end of our scripts.. Like variables, it is possible to explicitly destroy an object using the unset() function.

A copy, or not a copy.. Entire objects can be passed as arguments to functions, and can use all methods/variables within the function. Remember however.. like functions the object is COPIED when passed as an argument unless you specify the argument as a reference variable &$variable