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:
Workshop 1: OOP PHP Add data to your class: Note: The data/variables inside a class (ex: var name;) are called 'properties'.
Workshop 1: OOP PHP Add functions/methods to your class: 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:
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 <meta http-equiv="Content-Type" content="text/html; charset=iso8859-1" />
OOP in PHP
Workshop 1: OOP PHP Instantiate/create your object: $clifton = new Person(); Instantiation