PHP Object Oriented Classes and objects



Similar documents
CSCI 253. Object Oriented Programming (OOP) Overview. George Blankenship 1. Object Oriented Design: Java Review OOP George Blankenship.

Glossary of Object Oriented Terms

History OOP languages Year Language 1967 Simula Smalltalk

Facebook Twitter YouTube Google Plus Website

C++ INTERVIEW QUESTIONS

Description of Class Mutation Mutation Operators for Java

Web development... the server side (of the force)

3 Pillars of Object-oriented Programming. Industrial Programming Systems Programming & Scripting. Extending the Example.

Object Oriented Software Design II

Chapter 13 - Inheritance

Java Interview Questions and Answers

Agile Software Development

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

Fundamentals of Java Programming

Java (12 Weeks) Introduction to Java Programming Language

SE 360 Advances in Software Development Object Oriented Development in Java. Polymorphism. Dr. Senem Kumova Metin

Java Application Developer Certificate Program Competencies

Chapter 4 OOPS WITH C++ Sahaj Computer Solutions

AP Computer Science Java Subset

CIS 190: C/C++ Programming. Polymorphism

CS1002: COMPUTER SCIENCE OO MODELLING & DESIGN: WEEK 5

How To Design Software

Course Name: ADVANCE COURSE IN SOFTWARE DEVELOPMENT (Specialization:.Net Technologies)

Java Programming Language

CEC225 COURSE COMPACT

OpenCL Static C++ Kernel Language Extension

WORKSPACE WEB DEVELOPMENT & OUTSOURCING TRAINING CENTER

D06 PROGRAMMING with JAVA

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

Object oriented design process

El Dorado Union High School District Educational Services

Server-Side Scripting and Web Development. By Susan L. Miertschin

Compile-time type versus run-time type. Consider the parameter to this function:

C++FA 5.1 PRACTICE MID-TERM EXAM

Multimedia im Netz Online Multimedia Winter semester 2015/16. Tutorial 03 Major Subject

INTRODUCTION TO COMPUTER PROGRAMMING. Richard Pierse. Class 7: Object-Oriented Programming. Introduction

Object-Oriented Programming Lecture 2: Classes and Objects

Polymorphism. Problems with switch statement. Solution - use virtual functions (polymorphism) Polymorphism

java Features Version April 19, 2013 by Thorsten Kracht

Chapter 1 Fundamentals of Java Programming

Certified PHP Developer VS-1054

Instance Creation. Chapter

The Sun Certified Associate for the Java Platform, Standard Edition, Exam Version 1.0

Multichoice Quetions 1. Atributes a. are listed in the second part of the class box b. its time is preceded by a colon. c. its default value is

Constructor, Destructor, Accessibility and Virtual Functions

DIPLOMADO DE JAVA - OCA

Brent A. Perdue. July 15, 2009

UML for C# Modeling Basics

Object-Oriented Programming in Java

Specialized Programme on Web Application Development using Open Source Tools

Preet raj Core Java and Databases CS4PR. Time Allotted: 3 Hours. Final Exam: Total Possible Points 75

UNIVERSITY OF CALIFORNIA BERKELEY Engineering 7 Department of Civil and Environmental Engineering. Object Oriented Programming and Classes in MATLAB 1

Objectif. Participant. Prérequis. Remarque. Programme. C# 3.0 Programming in the.net Framework. 1. Introduction to the.


VB.NET - CLASSES & OBJECTS

16 Collection Classes

PHP Code Design. The data structure of a relational database can be represented with a Data Model diagram, also called an Entity-Relation diagram.

LAB4 Making Classes and Objects

Yosemite National Park, California. CSE 114 Computer Science I Inheritance

Introduction to Programming Block Tutorial C/C++

Agenda. What is and Why Polymorphism? Examples of Polymorphism in Java programs 3 forms of Polymorphism

How To Write A Program In Java (Programming) On A Microsoft Macbook Or Ipad (For Pc) Or Ipa (For Mac) (For Microsoft) (Programmer) (Or Mac) Or Macbook (For

The CLIPS environment. The CLIPS programming language. CLIPS production rules language - facts. Notes

Evaluating a new programming language

Inheritance, overloading and overriding

JAVA - INHERITANCE. extends is the keyword used to inherit the properties of a class. Below given is the syntax of extends keyword.

Lecture 7 Notes: Object-Oriented Programming (OOP) and Inheritance

CompuScholar, Inc. Alignment to Utah's Computer Programming II Standards

Practical Programming Methodology. Michael Buro. Class Inheritance (CMPUT-201)

13 Classes & Objects with Constructors/Destructors

Konzepte objektorientierter Programmierung

Checking Access to Protected Members in the Java Virtual Machine

JAVA - METHODS. Method definition consists of a method header and a method body. The same is shown below:

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

Johannes Sametinger. C. Doppler Laboratory for Software Engineering Johannes Kepler University of Linz A-4040 Linz, Austria

Overview. Elements of Programming Languages. Advanced constructs. Motivating inner class example

LAMP [Linux. Apache. MySQL. PHP] Industrial Implementations Module Description

The C Programming Language course syllabus associate level

ATM Case Study OBJECTIVES Pearson Education, Inc. All rights reserved Pearson Education, Inc. All rights reserved.

Chapter 5 Aspect Oriented Programming

a. Inheritance b. Abstraction 1. Explain the following OOPS concepts with an example

Part I. Multiple Choice Questions (2 points each):

INTRODUCTION TO OBJECTIVE-C CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 12 09/29/2011

Certified PHP/MySQL Web Developer Course

Comp 411 Principles of Programming Languages Lecture 34 Semantics of OO Languages. Corky Cartwright Swarat Chaudhuri November 30, 20111

Tutorial on Writing Modular Programs in Scala

How To Teach Object Oriented Programming At An Introductory Level Course

CS193j, Stanford Handout #10 OOP 3

International Journal of Advanced Research in Computer Science and Software Engineering

Java CPD (I) Frans Coenen Department of Computer Science

Bitter, Rick et al "Object-Oriented Programming in LabVIEW" LabVIEW Advanced Programming Techinques Boca Raton: CRC Press LLC,2001

Poor Man's Type Classes

CS193D Handout 06 Winter 2004 January 26, 2004 Copy Constructor and operator=

Transcription:

Web Development II Department of Software and Computing Systems PHP Object Oriented Classes and objects Sergio Luján Mora Jaume Aragonés Ferrero Department of Software and Computing Systems DLSI - Universidad de Alicante 1

Contents Introduction Class definition Object definition Constructor and destructor Inheritance Visibility Abstract, Final, static More OO features Introduction http://www.php.net/manual/en/language.oop.php http://www php net/manual/en/language oop5 php http://www.php.net/manual/en/language.oop5.php PHP offers principal features of OO Paradigm to the programmer: Encapsulation, Simple inheritance, Constructor and destructor methods Member privacity (visibility) Interfaces Overloading Abstraction etc. DLSI - Universidad de Alicante 2

Defining classes How to declare a class: class myclass public $attribute1, $attribute2, ; function Construct($arg1, $arg2, ) function Method1( ) function Method2( )... Class attributes must be declared explicitly depending on its visibility. It is the unique case where an identifier is due to declare. It is possible to use var in order to maintain compatibility with PHP4. A class only can have one constructor Creating an object $myobject = new myclass( ); Defining objects How to access to class members, operator arrow: -> (hyphen, right angle bracket) Outside the class: $myobject->attribute/method Inside the class: $this->attribute/method Scope resolution operator :: allows access to static, constant, and overridden members or methods of a class. Self:: allows to access to own static members and methods. parent:: allows to access to superclass static members and methods. DLSI - Universidad de Alicante 3

Constructor & destructor Class Constructor and Destructor have its own name: Constructor: construct() Destructor: destruct() In previous PHP 5 versions: The constructor was a member method with the same name of its class. There were not class destructors. Destructor will be invoked if: All references of an object disappear. An object is explicitly destroyed. <? class Person var $name, $surname; Example function Construct($n, $a) $this->name = $n; $this->surname = $a; $p1 = new Person( Homer', Simpson'); $p2 = new Person( Peter', Griffin');?> echo "$p1->surname, $p1->name<br />"; echo "$p2->surname, $p2->name<br />"; DLSI - Universidad de Alicante 4

Inheritance Simple inheritance can be realised by means of extends keyword. Simple inheritance means a class can only inherit from just one superclass (or base class) ATTENTION: base class constructor is not called automatically from the subclass constructor. You must invoke it explicitly. Inheritance systeax: class SubClass extends BaseClass... It is possible to overwrite all base class methods, whenever they are not final. We can access to base class methods or attributes using: parent:: Constructor & destructor: inheritance Child classes do not call base class constructor or destructor automatically They have to be invoked explicitly: parent:: construct() parent:: destruct() DLSI - Universidad de Alicante 5

Inheritance Example <?... class Client extends Person var $code; function Construct($n, $a, $c) parent:: Construct($n, $a); $this->code = $c; $c1 = new Client( Bender', Rodriguez', 123); echo "$c1->surmane, $c1->name: $c1->code<br />";?> One more example <?php class myclass function construct() print Parent class Constructor\n"; class childclass extends myclass function construct() parent:: construct(); print Child class Constructor\n"; $oparent = new myclass(); $ochild = new childclass();?> DLSI - Universidad de Alicante 6

Visibility The visibility of a property or method can be defined by prefixing the declaration with the keywords: public, protected or private. There are three levels of visibility: public: Public declared items can be accessed everywhere. protected: Protected limits access to inherited and parent classes (and to the class that defines the item). private: Private limits visibility only to the class that defines the item. Class attributes have to be declared with their visibility. If they are declared with var they will be public. By default, methods declared without visibility will be public. abstract classes and methods abstract: It is not allowed to create an instance of a class that has been defined as abstract Any class that contains at least one abstract method must also be abstract. Methods defined as abstract simply declare the method's signature they cannot define the implementation. When inheriting from an abstract class, all methods marked abstract in the parent's class declaration must be defined by the child DLSI - Universidad de Alicante 7

final: Final and Static A class defined final cannot be extended. Prevents child classes from overriding a method by prefixing the definition with final. Static: Todo método estático puede ser invocado sin tener que instanciar i un objeto de esa clase. $This no está accesible desde un miembro estático. More OO features Interface: allows to specify like a class templates. Method overloading: provides means to dynamically "create" members and methods Magic methods: You cannot have functions with these names in any of your classes unless you want the magic functionality associated with them. Object cloning: Creating a copy of an object with the same data and status. Clone and clone: spetial methods. Object comparison operator: === DLSI - Universidad de Alicante 8

Exercises Define a class cauthor with three attributes: name, surname and country. Define a class cbook with three attributes: title, number of pages and and object cauthor. Create all necessary methods: constructor, destructor, gets and sets. Program each class in a single file. Code a new PHP script including previous files and declare an object cbook with data and finally print its attribute values. DLSI - Universidad de Alicante 9