15
NuevoThoughts Technologies OOPs in PHP By Shubhendu Date:16-Mar-2013 V1 www.NuevoThoughts.com

Oop in-php

Embed Size (px)

Citation preview

NuevoThoughts Technologies

OOPs in PHP

By ShubhenduDate:16-Mar-2013 V1

www.NuevoThoughts.com

NuevoThoughts Technologies

www.NuevoThoughts.com

Summary of what we discussed:

What is OOP?

Why we use OOPs in PHP?

How do we use OOPs in PHP?

Where do we use OOPs in PHP?

Summary

NuevoThoughts Technologies

www.NuevoThoughts.com

What is OOP?:

Object Oriented Programming-Basically means Programming driven by Object

Object Oriented ProgrammingProcedural Programming

Programming Languages

Code controlling data(Code is important)

Data is to Access code(Data is important)

NuevoThoughts Technologies

www.NuevoThoughts.com

Object Oriented Programming:

Class: Programmer defined datatype include function & variables.

Object: Individual instances , used to access variable & method.

Functions: Set of code (specific tasks).

Variables: Data

InheritanceClass in class(parent & child class)PolymorphismOverloadingAbstractionEncapsulationConstructorDestructor

NuevoThoughts Technologies

www.NuevoThoughts.com

Why do we use OOP in PHP:

We can use PHP both as procedural and Object oriented language.

Now, question is why we use OOP concept in PHP?:

Reasons:

Flexibility

Reduce source code by more than 99.9%.

Much easier in implementing security.

Makes coding more organized.

Help to work in team easily.

NuevoThoughts Technologies

www.NuevoThoughts.com

Flexibility:

Easily extend our PHP code whenever we require.

Member variables & functions are easily accessible from any where in the code.

Example:in abc.php:class sample

{public $var;

function sample-f(){some code………} }

in abc1.php:<?php include abc.php$obj=new sample();$obj->var=738$obj->sample-f();?>

NuevoThoughts Technologies

www.NuevoThoughts.com

Reduce Source Code by more than 99.9%:

When we create big project which require lots of files, it reduces our lots of work.

In procedural PHP when we require same variable in more than one file, we copy paste that variable or function .But in case of OOP PHP we just put code in class , by object we can call them.

Example: in abc.phpclass s1{public $var;function f1(){//code}}

*in abc1.php *in abc2.php<?php include ‘abc.php’; <?php include ‘abc.php’;$obj=new s1();$obj->var=“hello”;?> $obj=new s1();

$obj->var=“hi”; ?>

NuevoThoughts Technologies

www.NuevoThoughts.com

Much easier in implementing security:

Security is most vital requirement in web development.

OOPs provide security by using object, all data transferred in terms of objects.

Example:In abc.php in abc1.phpClass a <?php {$var; include ‘abc.php’; public Function f1($val) $obj=new a();{If($val==NULL) $obj->f1(“sss”);{ echo $obj->val;This->val=“hello1”; //$obj->f2(“123”);}} //echo $obj->val;Public Function f2($val) ?>{if($val==123)This->val=“hello2”;}}

NuevoThoughts Technologies

www.NuevoThoughts.com

Makes coding much easier:

OOP makes coding much easier to understand and clean.

Help to work in team easily:

Its important to have same method, implementations, algorithm in teamFor a project.

NuevoThoughts Technologies

www.NuevoThoughts.com

How do we use OOP in PHP?:

Step 1: Create two or more PHP files:(assuming two)Index.phpClass.php Step 2: Put all OOP code in class.php;Class a(){$var;Function f($val)

{this->var=$val;

}}Step 3:create object and access OOP functions/variables from another file.<?phpInclude class.php;$obj=new a();$obj->var=“hi”;$obj->f(“hello”);?>

NuevoThoughts Technologies

www.NuevoThoughts.com

The Principle of Model view Controller:

INPUT->PROCESS->OUTPUTCONTROLER->MODEL->VIEW

view

model

controller

User

uses

manipulatesupdates

sees

application

NuevoThoughts Technologies

www.NuevoThoughts.com

Model:

A model is an object representing data or even activity.

The model manages the behavior and data of the application domain.

The model is the piece that represents the state.

View:

A view is some form of visualisation of the state of the model.

The view is responsible for mapping graphics onto a device.

Controllers:

A controller offers facilities to change the state of the model.

A controller is the means by which the user interacts with the application.

NuevoThoughts Technologies

www.NuevoThoughts.com

Where do we use OOPs in PHP:

Often use for creating CMS(Content management system).

Secure application.

For big projects.

NuevoThoughts Technologies

www.NuevoThoughts.com

Summary:

PHP can be used both as procedural and Object-oriented.

OOP provides flexibility, security, easiness etc to programmer.

MVC is totally based on OOPs concept.

CMS Is designed on MVC model

NuevoThoughts Technologies

Thanks

www.NuevoThoughts.com