introduction to php oop

34
INTRODUCTION TO PHP

Upload: fakhrul-hasan

Post on 19-Jan-2017

401 views

Category:

Engineering


5 download

TRANSCRIPT

Page 1: Introduction to PHP OOP

INTRODUCTION TO PHP

Page 2: Introduction to PHP OOP

PREPARED BY

FAKHRUL HASAN

COMPUTER PROGRAMMER

DAFFODIL INTERNATIONAL UNIVERSITY

EXPERTISE AREA:

WEB DESIGNING, WEB DEVELOPMENT, PHP, C#, ASP.NET, MYSQL AND MS SQL.

Page 3: Introduction to PHP OOP

TOPICS

WHAT IS PHP?

HISTORYHOW WORKSWHAT PHP CAN DO?WHY PHP USED?HOW STUDENTS GET BENEFITED

PHP – BASIC PROGRAMMING

PHP – OBJECT ORIENTED PROGRAMMING

GET AND POST METHODSESSIONS AND COOKIES

Page 4: Introduction to PHP OOP

PHP (Hypertext Pre-Processor) is a widely-used open source

scripting language that is used for web development.

It can be embedded in HTML Document.

Currently, PHP support both Procedural and OOP

WHAT IS PHP?

Page 5: Introduction to PHP OOP

HISTORY

PHP/FI – Personal

Home Page –

Developed in1994 by Rasmus Lerdorf.

Support: DBM,

mSQL, and Postgres95 databases, cookies,

user-defined function

support, and much more.

PHP 3 – Hyper Text Pre-

Processor – Released in

1998Developed by Andi, Rasmus,

and Zeev.Features: Multiple database

support, OOP Support

PHP 4 –Developed by

Zeev And ANDI. Designed based on Zend Engine.

Released in 2000.

Support Multiple Databases, Many Web

Servers, HTTP Sessions etc.

PHP 5 – Released in 2004. Fully

OOP, Support Multiple

Databases and some advance features.

Page 6: Introduction to PHP OOP

HOW WORKS

PHP Pre-Processor

Database

Client Browser

Page Request

HTML

HTM

L

Passed

Put Data

Get Data

Page 7: Introduction to PHP OOP

WHAT PHP CAN DO?

Server Side Scripting: • Main target field of PHP.

Command Line Scripting: • PHP scripts can run on command prompt without server.

Writing Desktop Application:

• Not best. But can be developed using PHP-GTK php extension.

Page 8: Introduction to PHP OOP

WHY PHP USED?

Easy to use. Code is embedded into HTML.

Easy to built informative website, e-commerce, high-level management system and so on.

Runs on almost any web server, many databases and operating systems.

Supported Web-Servers: Apache, Microsoft IIS, Netscape Enterprise Server.

Supported Operating Systems: Mac-Os, Windows, Linux (Solaries) etc.

Database Supported: MS-SQL, MySQL, Oracle, PostgreSQL, SQLite, FilePro, dBase etc.

Page 9: Introduction to PHP OOP

HOW STUDENTS GET BENEFITED WITH LEARNING PHP

Students who learn PHP for web-development will get following advantages

Cost Effective Development Easily Integration

Flexible to use Available Learning Resources

Page 10: Introduction to PHP OOP

PHP – BASIC PROGRAMMING

Variables: Variables are used to store data. PHP supports different data-type. But it is not necessary to declare data-type. PHP uses $ sign to declare a variable.Syntax: <?php $a = “hello php”; ?>

Arrays: Array is a process to store multiple values to a single variable in php. The values are identified by key.Syntax: <?php $students = array(‘hasan’, ‘kabir’); ?> or

<?php $students = array(‘first’=>’hasan’, ‘second’=>’kabir’); ?>

Page 11: Introduction to PHP OOP

PHP – BASIC PROGRAMMINGLoop:

A sequence of instructions that repeated until reached to a certain condition. Loops in PHP: for, while, do..while and foreach.

Example:

Page 12: Introduction to PHP OOP

PHP – BASIC PROGRAMMING

Functions: Functions are special block of code which can re-use again and again.Used to minimize code and better performance.Basically Two Types: Built-in functions, User-defined functions.Functions are executed, when it is called.Sample Example:

Page 13: Introduction to PHP OOP

PHP- BASIC PROGRAMMING

Conditional Statements:Conditional Statements are used to perform different action on different decision.Conditional Statements are if, if…else, if…..else if…..else if….else, switch…caseExample:

Page 14: Introduction to PHP OOP

OBJECT ORIENTED PROGRAMMING PHP

Classes and Objects

Methods and Properties Scope

Inheritance

Static methods and properties Constants

Abstraction and interfaces Overriding Object

Iteration

Concept of OOP is to move the application architecture closer to real world. The OOP features of PHP is described below:

Page 15: Introduction to PHP OOP

OBJECT ORIENTED PROGRAMMING PHP

Classes and Objects Classes are type of entity and objects are single units of a given class If human is a class, then Mr. Karim, Rahim are objects of human class. Examples:

Page 16: Introduction to PHP OOP

CONSTRUCTOR

Each class has one constructor. Constructor is called when object is initialized.

Page 17: Introduction to PHP OOP

CONSTRUCTOR

Page 18: Introduction to PHP OOP

PROPERTIES

A class may have unlimited number of properties

Page 19: Introduction to PHP OOP

ACCESS MODIFIEREach property and method has access modifier/scope in PHP class. It defines who can access it. Access modifiers are:

Public •It can be accessed from any outside file through object

Protected•Protected can be accessed by descendant class

Private•Private can be accessed by a class itself.

Page 20: Introduction to PHP OOP

INHERITANCE• A class can inherit another class.

• When a class inherit another class, it inherit all public an protected properties of inherited class. But can’t inherit private property.

Page 21: Introduction to PHP OOP

STATIC METHOD AND PROPERTY

Defining static to a property or method makes them accessible without creating object.Class can access static property with self keyword. Outside world can access it with class name. Example:

Page 22: Introduction to PHP OOP

CONSTANTSNormally constants are declared in PHP with define() function.

But in a class constants are declared with const keyword and not using $ sing. Class constant can’t be called with object.

Example:

Page 23: Introduction to PHP OOP

ABSTRACT CLASS AND METHOD

Abstract class can defined with abstract keyword and can’t be instantiated.

This type of class must be used by inheritance.

Abstract class may have several abstract method.

If abstract class has abstract method definition. Child class must have the implementation of abstract method.

Page 24: Introduction to PHP OOP

ABSTRACT CLASS AND METHOD

Examples:

Page 25: Introduction to PHP OOP

INTERFACE

Interfaces allow what a child class must implement.

No methods in interface have implementation.

Classes can implement interface instead of extends.

Interface can only have public method.

Page 26: Introduction to PHP OOP

INTERFACES

Example:

Page 27: Introduction to PHP OOP

OVERRIDINGWhen a class inherit another class, it can declare methods that override parent class method.

Two class method names are same, parameter may differ. An Example is following:

Page 28: Introduction to PHP OOP

OBJECT ITERATIONPHP provides facility to iterate an object as item of an array. Example is following:

Page 29: Introduction to PHP OOP

NAMESPACEPHP Namespace after 5.3 version allow create class with same name in same application or page under different namespaces. Example Code is following:

Page 30: Introduction to PHP OOP

PHP – GET AND POST VARIABLE

GET and POST create an array with key and value pair, where key contains name of the form field and value contains the value of the field.

• GET is treated as $_GET in PHP, which holds all information passed through URL as array.

• Information sent through GET Method is visible to users.

• GET has limitation highest 2000 characters

GET

• POST is treated as $_POST in PHP, which holds all information passed through form post as array.

• Information sent through POST Method is not visible to users.

• POST has no limitation.

POST

Page 31: Introduction to PHP OOP

COOKIESCookie is piece of text file that web

servers can store on user’s

browser.

It allows server to

store information to user machine and later

retrieve it.Can store

file up to 4 kb.

Maximum 20 cookies

can be stored on user’s PC per server.

Page 32: Introduction to PHP OOP

SESSIONSSession is a mechanism to store information to be used in multiple pages on the server.

PHP Session variable holds information about a single user (browser) and it available on multiple pages.

Basically made up of two components. Client Side Session ID and Server Side Session Data.

To use session in PHP, developer must call the function session_start(); at the top.

Page 33: Introduction to PHP OOP

PHP FRAMEWORKS

Some popular frameworks of PHP are following:

LaravelSymfonyCode IgniterYiiCake PHPZendPhalcon

Popular CMS build using PHP:WordpressJoomlaDrupalExponent CMSMagentoPHP Nuke

Page 34: Introduction to PHP OOP

QUESTIONS??