Seminar

  • Uploaded by: api-26932678
  • 0
  • 0
  • July 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Seminar as PDF for free.

More details

  • Words: 1,618
  • Pages: 32
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

Workshop 1: OOP PHP The 'new' keyword

To create an object out of a class, you need to use the 'new' keyword. $clifton = new Person(); $rolysent = new Person;

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

Workshop 1: OOP PHP Set an objects properties: set_name("clifton Alegarme"); $rolysent->set_name("Rolysent Paredes"); ?>

Workshop 1: OOP PHP Accessing an object's data

When accessing methods and properties of a class, you use the arrow (->) operator. 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(); ?>

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: name = $new_name; } function get _name(){ return $this->name; } } ?>

Workshop 1: OOP PHP How to access the Private/Public Properties: 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: 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( ?>

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 = "
".Person::get_name()."
"; $this->link .= Person::get_sss_id()."
"; $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

Related Documents

Seminar
April 2020 44
Seminar
July 2020 34
Seminar
June 2020 38
Seminar
June 2020 34
Seminar
May 2020 37
Seminar
November 2019 30