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.

Slides:



Advertisements
Similar presentations
» PHP arrays are lists of values stored in key-value pairs. » Uses of arrays: Many built-in PHP environment variables. Database functions use arrays.
Advertisements

Introduction to PHP MIS 3501, Fall 2014 Jeremy Shafer
NMD202 Web Scripting Week5. What we will cover today PHPmyAdmin Debugging – using print_r Modifying Data PHP (cont.) 4D Methodology File and IO operations.
OBJECT-ORIENTED PROGRAMMING. What is an “object”? Abstract entity that contains data and actions Attributes (characteristics) and methods (functions)
Object-Oriented PHP (1)
Road Map Introduction to object oriented programming. Classes
PHP (2) – Functions, Arrays, Databases, and sessions.
VBA Modules, Functions, Variables, and Constants
ASP.NET Programming with C# and SQL Server First Edition
PHP Programming with MySQL Slide 11-1 CHAPTER 11 Developing Object-Oriented PHP.
INTERNET APPLICATION DEVELOPMENT For More visit:
Lecture 6 – Form processing (Part 1) SFDV3011 – Advanced Web Development 1.
UFCEUS-20-2 : Web Programming Lecture 5 : Object Oriented PHP (1)
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Chapter 10 Managing Data with ASP.NET. ASP.NET 2.0, Third Edition2.
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
1 Overview of Databases. 2 Content Databases Example: Access Structure Query language (SQL)
9 Chapter Nine Compiled Web Server Programs. 9 Chapter Objectives Learn about Common Gateway Interface (CGI) Create CGI programs that generate dynamic.
Class 1Intro to Databases Goals of this class Understand the architecture behind web database applications Gain a basic understanding of what relational.
Accessing MySQL with PHP IDIA 618 Fall 2014 Bridget M. Blodgett.
PHP and MySQL CS How Web Site Architectures Work  User’s browser sends HTTP request.  The request may be a form where the action is to call PHP.
Object Oriented Programming in PHP. Topics Quick OOP Review Classes Magic Methods Static Methods Inheritance Exceptions Interfaces Operators Type Hinting.
Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.
Mr. Justin “JET” Turner CSCI 3000 – Fall 2015 CRN Section A – TR 9:30-10:45 CRN – Section B – TR 5:30-6:45.
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.
Open Source Software Unit – 3 Presented By Mr. R.Aravindhan.
Triggers and Stored Procedures in DB 1. Objectives Learn what triggers and stored procedures are Learn the benefits of using them Learn how DB2 implements.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
CHAPTER 9 PHP AND MYSQL. A POSSIBLE SITE CONFIGURATION Application Folder index.php includes (folder)header.phpfooter.phpstyle.cssmodel (folder)mysqli_connect.php.
CISC 3140 (CIS 20.2) Design & Implementation of Software Application II Instructor : M. Meyer Address: Course Page:
Programming in C++.
Chapter 2 Functions and Control Structures PHP Programming with MySQL 2 nd Edition.
Just a Little PHP Programming PHP on the Server. Common Programming Language Features Comments Data Types Variable Declarations Expressions Flow of Control.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
1 Data Manipulation (with SQL) HRP223 – 2010 October 13, 2010 Copyright © Leland Stanford Junior University. All rights reserved. Warning: This.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
RECITATION 4. Classes public class Student { } Data Members public class Student { private String name; public String id; }
Date : 02/03/2014 Web Technology Solutions Class: OOP PHP, Design Patterns and CRUD.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
David Lawrence 7/8/091Intro. to PHP -- David Lawrence.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
Just a Little PHP Programming PHP on the Server. Common Programming Language Features Comments Data Types Variable Declarations Expressions Flow of Control.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
Learningcomputer.com SQL Server 2008 –Views, Functions and Stored Procedures.
>> PHP: MySQL & CRUD. R ecall Database Tables Records is composed of Operations (CRUD) Create Retrieve Update Delete DBMS Access Control MySQL phpMyAdmin.
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.
Working With Database Library And Helpers. Connecting to your Database First you need to set parameters in you database.php file residing in config folder.
PHP Reusing Code and Writing Functions 1. Function = a self-contained module of code that: Declares a calling interface – prototype! Performs some task.
 We have created our two separate 'person' objects, we can set their properties using the methods (the setters) we created.  class person {  var $name;
Chapter 2 - OOP Maciej Mensfeld Presented by: Maciej Mensfeld More about OOP dev.mensfeld.pl github.com/mensfeld.
 First thing we need to do is create two PHP pages:  index.php  class_lib.php  OOP is all about creating modular code, so our object oriented PHP.
CS 440 Database Management Systems Stored procedures & OR mapping 1.
CPS120: Introduction to Computer Science Lecture 16A Object-Oriented Concepts.
PHP (Session 2) INFO 257 Supplement.
Introduction to Object-oriented Programming
Web Technology Solutions
Classes & Objects There are two main programming paradigms: Procedural Object-Oriented Up to now, everything we have done has been procedural.
Introduction to CodeIgniter (CI)
University of Central Florida COP 3330 Object Oriented Programming
Server-Side Application and Data Management IT IS 3105 (Spring 2010)
Chapter 11 Developing Object-Oriented PHP PHP Programming with MySQL Revised by A. Philipp – Spring 2010 (Rev SP’11)
CIS16 Application Development Programming with Visual Basic
Object-Oriented Programming in PHP
Tonga Institute of Higher Education
Tutorial 6 PHP & MySQL Li Xu
Object-Oriented Programming
PHP Forms and Databases.
Object-Oriented PHP (1)
Classes and Objects Imran Rashid CTO at ManiWeber Technologies.
Presentation transcript:

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 and Disadvantages of OOP Parts of a Class – Properties – Methods – Constructors Creating an Object Passing data to an object when it is first created or to the constructor Passing data to a property Passing data to a method Getting data from a property Getting data from a method Naming issues/requirements Printing PDO vs mysql_link Sending multiple variables of data back from a method Parsing of the returned data

What is a Class A Class is a template of code that is used to create an Object. Objects do the work, Classes are the design of the code to do the work.

What is an Object An Object is a Instantiation of a Class. If you have only one Object from a class it is called a Singleton If you have multiple Objects from a class it is best to store the objects in an array.

Advantages and Disadvantages of OOP If you have a large project in PHP or if you have a project that is in a constant state that it is running (other then PHP) then you should use OOP. If you have a small project that does not use a lot of reused code then Procedural programming is better as it is lighter.

Parts of a Class Properties – If you have variables that are to be shared between methods it is best to declare them as properties. – As of PHP5 properties start with private, public or protected – The visibility of private, public or protected is different then ActionScript – public $name = “Doug”; – They are reference with $this->name in methods – They are references from the main file with $oObject->name ;

Parts of a Class Methods – Methods are functions that work on data. – As of PHP5 methods start with private, public or protected – The visibility of private, public or protected is different then ActionScript – public function myToDo () { } – They are reference with $this->myToDo () in other methods – They are referenced from the main file with $oObject->myToDo () ;

Parts of a Class Constructors – Constructors are special methods that are called when an Object is created. – As of PHP5 Constructors are named as __construct() – The visibility of private, public or protected is different then ActionScript – public function __construct () { } – I usually use the constructor to set default properties for the object such as connecting to the Database.

Creating an Object You first need to include the Class with a require_once to reference the file the class is in. I always start the name of my objects with a lowercase o and then camel case the rest of the name. $oObject Use the new command to create the object $oObject = New Bicycles();

Passing data to an object when it is first created or to the constructor When creating the object from the class pass variable data in the brackets to the constructor. $oObject = New Bicycles($id); Class Bicycles { public $id = “”; public function __construct ($id) { $this->id = $id; }

Passing data to a property $oObject->id = $id; // outside object $oObject->id = “22”; // outside object $this->id = $id; // from within object $this->id = “22”; // from within object

Passing data to a method Data id passed in the brackets of the method name; $oObject->getBicycle($id); // outside object $oObject->getBicycle(“22”); // outside object $this->getBicycle($id); // from within object $this->getBicycle(“22”); // from within object

Getting data from a property $id = $oObject->id ; // outside object print $oObject->id ; // outside object $id= $this->id; // Do not do this as you just need to reference the property only from within object

Getting data from a method $title = $oObject->getBicycleTitle($id); // outside object print $oObject->getBicycleTitle($id); // outside object $title = $this->getBicycleTitle($id); // from within object (Never Print from with in a Method except for debugging)

Naming issues/requirements When naming properties and methods use camel case When naming variables use lowercase with underscores. The use of objects reduces name space issues Start all class names with a capital Start all objects with a lower case o

Printing NEVER use print or echo with a class or object (printing can only be used for debugging) It is bad form data does not print where it needs to be seen.

PDO vs mysql_link The procedural way of connecting to a database is to create a resource link such as $mysql_link For OOP use the PDO way which is PHP Database Objects Create a property in the __construct that has the reference to the PDO. public $pDbh ; // define constructor public function __construct() { require_once("connect_dbh.php"); $this->pDbh = &$dbh; }

PDO vs mysql_link connect_dbh.php <?php try { $user = ”youraccount"; $pass = ”yourpassowrd"; $dbh = new PDO('mysql:host=localhost;dbname=yourdatabase', $user, $pass); } catch (PDOException $e) { print("Error: ".$e->getMessage()." "); } ?>

PDO vs mysql_link SELECT $query = "SELECT title FROM bicycles WHERE id = '$id'"; foreach ($this->pDbh->query($query) as $row) { $title = stripslashes($row[0]); }

PDO vs mysql_link INSERT, DELETE or UPDATE $query = ”UPDATE bicycles SET title = ‘$title’ WHERE id = '$id'"; $this->pDbh2->query($query);

Sending multiple variables of data back from a method You can use a return command to send a single variable back from the calling method. public function addThis ($a,$b) { $total = $a + $b; return $total; }

Sending multiple variables of data back from a method To send multiple variables back in a return you need to place the variables into an array. public function getBicyclesList () { $data = “”; $query = "SELECT id, title FROM bicycles ORDER BY id ”; $x = 0; foreach ($this->pDbh->query($query) as $row) { $id= stripslashes($row[0]); $title = stripslashes($row[1]); $data[$x][‘id’] = $id; $data[$x][‘title’] = $title; ++$x; } return $data; }

Parsing of the returned data If the data is a simple structure then just print it or place into a variable. print $oObject->addThis(12, 6) ; // prints 18

Parsing of the returned data If the data is an array structure then you need to parse the array. $bikeListArray = $oBicycles->getBicyclesList(); foreach ($bikeListArray as $key=>$bike) { $id = $bike[‘id’]; $title = $bike[‘title’]; print(“ $title ”); }

File Names Each class resides in it’s own file Each file has the name class in it. The file begins with the name of the class bicycles.class.php These files will reside in the inc/php folder