P. 1
Seminar

Seminar

|Views: 165|Likes:
Publicado porapi-26932678

More info:

Published by: api-26932678 on Dec 05, 2009
Direitos Autorais:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PPT, PDF, TXT or read online from Scribd
See more
See less

03/18/2014

pdf

text

original

OOP PHP with MySQL

By: CLIFTON S. ALEGARME

– 1/31/2009

Objectives of this Lesson
  

To be able to know the basics OOP To be able to know the basics OO PHP To be able to know the Pros and Cons of OO PHP To be able to know in creating Objects and Classes (Properties & Methods) To be able to know in creating OO PHP classes in connecting in MySQL.

Introduction to OOP
The hardest thing to learn (and teach btw,) in object oriented PHP … is the basics. But once you get them under-your-belt, the rest will come much, much easier. People run into confusion when programming because of some lack of understanding of the basics. With this in mind, we are going to slowly go over key OOP principles while creating our own PHP objects. With this knowledge, you will be able to explore OOP further.

What is OOP?
Object-Oriented Programming (OOP) is a type of programming added to php5 that makes building complex, modular and reusable web applications that much easier. With the release of php5, php programmers finally had the power to code with the 'big boys'. Like Java and C#, php finally has a complete OOP infrastructure.

History of OO PHP
-It was created on 1995 by Rasmus Lerdof. Rasmus. -PHP 3 was released in 1997 and Modified by Andy Gutmans and Zeev Suraski and PHP 3 project robust Apache Module. -PHP 3 was introduce the first elements of OO. -PHP 4 Andi Gutmans and Zeev Suraski once again re-architected PHP from the ground up and it was built upon a piece of technology called Zend Engine. -PHP 4 also built on the earlier OOP features of PHP 3 with the introduction of classes.

Cont.. History of OO PHP
-PHP 5 improved support of OOP and Introduce some features common to other languages such as Java like “try/catch error and exception handling.” -PHP 5 also introduced new extensions aimed at easing the storage and manipulation of data. Significant new features include SimpleXML for handling XML documents, and SQLite, an embedded basic and easy to use database interface.

Why learn OO PHP
OO PHP code is much more reusable because by its' very nature, it is modular. OO PHP is easier to update. Again, because PHP code is organised into objects. OO PHP makes team programming much easier to manage. OO PHP makes larger projects much easier to manage. OO PHP makes creating code libraries much easier.

Cont. Why learn OO PHP
Knowing OO PHP will make working with many opensource PHP libraries much easier OO PHP programmers typically make more money and will be able to work on more projects. Since object oriented concepts are the same in all OO languages, once you learn OOP in PHP.

Downside of OO PHP
Object oriented PHP has 3 small disadvantages:

-It is harder to learn than traditional (procedural) PHP. -OO PHP will run a little slower than traditional PHP. -OO PHP projects require more code than traditional PHP projects when the projects are small and just starting out. That is to say, if you are only writing a small 2 or 3 page script, you may just want to go with old-school PHP.

OBJECT and CLASSES
What is an Object? An object is just PHP code wrapped up in package. Objects sort of look like functions (in code,) but are a lot more powerful. Contrast this to classic PHP, where you would have a bunch of functions, variables and other code floating around willy-nilly. OO PHP is about creating modular code that is contained in virtual containers called: objects.

OBJECT and CLASSES
Objects: Functions on steroids: PHP objects kind of look like functions because PHP objects will have many of the same sort of things in them … things like: 1. variables 2. conditionals 3. functions

OBJECT and CLASSES
What is a ‘CLASS’? A template (for an object) is called a 'class'. Classes are the blueprints for objects in PHP. Actually, classes are used in every OO programming language ever invented! They are one of the fundamental constructs in object-oriented programming.

OBJECT and CLASSES
How objects are created: 1. You create a class that 'describes' an object. Much in the same way a blueprint 'describes' a building. Instead of room dimensions etc. PHP classes / blueprints details a bunch of things about an object: a. Variables it contains b. Functions it contains c. Objects are much more powerful complex when compared to functions. and

OBJECT and CLASSES
How objects are created: continue… 2. Once you've defined your class, you are ready to tell the PHP engine to actually create a class from your blueprint. There are special commands (PHP code,) that tell the PHP engine to create an object from the 'blueprint' you described/outlined in a class. 3. When the PHP script is run (with the code that instructs PHP to create an object … based on the class,) PHP actually creates a living, breathing object based on your class.

Workshop 1: OOP PHP

Create this two PHP pages: index.php class_lib.php

Workshop 1: OOP PHP
Create a PHP class: <?php class Person { } ?>

Workshop 1: OOP PHP
Add data to your class: <?php class Person { var $name; } ?> Note: The data/variables inside a class (ex: var name;) are called 'properties'.

Workshop 1: OOP PHP
Add functions/methods to your class:
<?php class Person { var $name; function set_name($new_name){ $this->name = $new_name; } function get_name(){ return $this->name; } } ?> Note: Don't forget that in a class, variables are called 'properties'.

Workshop 1: OOP PHP
Getter and setter functions:
<?php class Person { var $name;

function set_name($new_name){ $this->name = $new_name; } function get_name(){ return $this->name; }

} ?>

Workshop 1: OOP PHP
The '$this' variable $this->name = $new_name;
The $this is a built-in variable (built into all objects) which points to the current object. Or in other words, $this is a special self-referencing variable. You use $this to access properties and to call other methods of the current class.

function get_name() { } return $this->name;

Workshop 1: OOP PHP
Include your class in your main PHP page
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso8859-1" /> <title>OOP in PHP</title> <?php include("class_lib.php"); ?> </head> <body> </body> </html>

Workshop 1: OOP PHP
Instantiate/create your object:
<?php include("class_lib.php"); ?> </head> <body> $clifton = new Person(); Instantiation </body> </html>

Workshop 1: OOP PHP
The 'new' keyword
To create an object out of a class, you need to use the 'new' keyword. <?php include("class_lib.php"); ?> </head> <body> $clifton = new Person(); $rolysent = new Person; </body> </html>

Note: When creating an object, be sure not to quote the class name

Workshop 1: OOP PHP
Set an objects properties:
<?php include("class_lib.php"); ?> </head> <body> <?php $clifton = new Person(); $rolysent = new Person; $clifton->set_name("clifton Alegarme"); $rolysent->set_name("Rolysent Paredes"); ?> </body> </html>

Workshop 1: OOP PHP
Accessing an object's data
When accessing methods and properties of a class, you use the arrow (->) operator.
<?php include("class_lib.php"); ?> </head> <body> <?php $clifton = new Person(); $rolysent = new Person; $clifton->set_name(“Clifton Alegarme"); $rolysent->set_name(“Rolysent Paredes"); echo "Clifton’s full name: " . $clifton->get_name(); echo "Roly’s full name: " . $rolysent->get_name(); ?> </body> </html>

Note: The arrow operator (->) is not the same operator used withssociative arrays: =>.

Workshop 1: OOP PHP
Directly accessing properties - don't do it!
You don't have to use methods to access objects properties; you can directly get to them using the arrow operator (->) and the name of the variable. For example: with the property $name (in object $stefan,) you could get its' value like so: $clifton->name. // directly accessing properties in a class is a no-no. echo "Clifton’s full name: " . $clifton->name; Though doable, it is considered bad practice to do it because it can lead to trouble down the road. You should use getter methods instead

Workshop 1: OOP PHP
For the rest of this Workshop, I'm going to stop reminding you that: • Functions = methods • Variables = properties

Workshop 1: OOP PHP
Restricting access to properties using 'access modifiers'
One of the fundamental principles in OOP is 'encapsulation'. The idea is that you create cleaner better code, if you restrict access to the data structures (properties) in your objects. You restrict access to class properties using something called 'access modifiers'. There are 3 access modifiers: 1. public 2. private 3. protected

Public is the default modifier.

Workshop 1: OOP PHP
Example:
<?php class Person { var $name; private $sss_id = “990337”; protected $account_no = “123456”; function set_name($new_name){ $this->name = $new_name; } function get _name(){ return $this->name; } } ?>

Workshop 1: OOP PHP
How to access the Private/Public Properties: <?php class Person { var $name; private $sss_id = “990337”; protected $account_no = “123456”; function set_name($new_name){ $this->name = $new_name; } function get _name(){ return $this->name; } function get_sss_id(){ return $this->sss_id; } function get_account_no(){ return $this->account_no; } } ?>

Main Page: <?php include("class_lib.php"); ?> </head> <body> <?php $clifton = new Person(); $rolysent = new Person; $clifton->set_name(“Clifton Alegarme"); $rolysent->set_name(“Rolysent Paredes"); echo "Clifton’s full name: " . $clifton>get_name(); echo "Roly’s full name: " . $rolysent>get_name(); echo “SSS ID:” . $clifton->get_sss_id(); echo “Account no:” . $clifton->get_account_no( ?> </body> </html>

Workshop 1: OOP PHP
Reusing code the OOP way: Inheritance
Inheritance is a fundamental capability/construct in OOP where you can use one class, as the base/basis for another class … or many other classes.
class employee extends Person{ var $link; function get_All(){ $this->link = "<br>".Person::get_name()."<br>"; $this->link .= Person::get_sss_id()."<br>"; $this->link .= Person::get_account_no(); return $this->link;}

}

'extends' is the keyword that enables inheritance

Workshop 2
OOP in PHP connect to MySQL OOP

You're Reading a Free Preview

Descarregar
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->