introduction to php oop

38
Introduction to PHP OOP in PHP Week 11, day1

Post on 11-Sep-2014

439 views

Category:

Technology


3 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Introduction to php oop

Introduction to PHP

OOP in PHP

Week 11, day1

Page 2: Introduction to php oop

What is OOP?

• Object-oriented programming (OOP) is a style of programming

that focuses on using objects to design and build applications.

• Class : is the base design of objects

• Object : is the instance of a class

• No memory is allocated when a class is created.

• Memory is allocated only when an object is created.

Page 3: Introduction to php oop

Java PHP

class shape

{

private int width;

private int height;

public int calculateArea()

{

return width * height;

}

}

class shape

{

private $width;

private $height;

public function calculateArea()

{

return $width * $height;

}

}

Classes

Page 4: Introduction to php oop

Java PHP

class shape

{

private int width;

private int height;

public int calculateArea()

{

return width * height;

}

}

shape rectangle = new shape();

class shape

{

private $width;

private $height;

public function calculateArea()

{

return $width * $height;

}

}

$rectangle = new shape();

Objects

Page 5: Introduction to php oop

Objects in detail

• An object is always passed by reference rather than by value.

example:

$a = new shape();

$b = $a();

In this case, both $a and $b will point to the same object, even

though we didn’t specify that we wanted this to happen by means

of any special syntax.

1000

1000

Heap

1000$a

$b

Page 6: Introduction to php oop

Inheritance in PHP

• One of the key fundamental concepts of OOP is inheritance.

• This allows a class to extend another class, essentially adding new

methods and properties, as well as overriding existing ones as

needed. Example as follows

Page 7: Introduction to php oop

class a {

function test()

{

echo "a::test called";

}

function func()

{

echo "a::func called";

}

}

class b extends a {

function test()

{

echo "b::test called";

}

}

class c extends b {function test(){

parent::test();}

}class d extends c {

function test(){

b::test();}

}$a = new a();$b = new b();$c = new c();$d = new d();$a->test(); // Outputs "a::test called"$b->test(); // Outputs "b::test called"$b->func(); // Outputs "a::func called"$c->test(); // Outputs "b::test called"$d->test(); // Outputs "b::test called"

Page 8: Introduction to php oop

class a {

function test()

{

echo "a::test called";

}

function func()

{

echo "a::func called";

}

}

class b extends a {

function test()

{

echo "b::test called";

}

}

class c extends b {function test(){

parent::test();}

}class d extends c {

function test(){

b::test();}

}$a = new a();$b = new b();$c = new c();$d = new d();$a->test(); // Outputs "a::test called"$b->test(); // Outputs "b::test called"$b->func(); // Outputs "a::func called"$c->test(); // Outputs "b::test called"$d->test(); // Outputs "b::test called"

Parent keyword is used to access the immediate parents properties and

methods

Page 9: Introduction to php oop

class a {

function test()

{

echo "a::test called";

}

function func()

{

echo "a::func called";

}

}

class b extends a {

function test()

{

echo "b::test called";

}

}

class c extends b {function test(){

parent::test();}

}class d extends c {

function test(){

b::test();}

}$a = new a();$b = new b();$c = new c();$d = new d();$a->test(); // Outputs "a::test called"$b->test(); // Outputs "b::test called"$b->func(); // Outputs "a::func called"$c->test(); // Outputs "b::test called"$d->test(); // Outputs "b::test called"

We can use the parent class’s name for

accessing its properties and methods

Page 10: Introduction to php oop

class a {

function test()

{

echo "a::test called";

}

function func()

{

echo "a::func called";

}

}

class b extends a {

function test()

{

echo "b::test called";

}

}

class c extends b {function test(){

parent::test();}

}class d extends c {

function test(){

b::test();}

}$a = new a();$b = new b();$c = new c();$d = new d();$a->test(); // Outputs "a::test called"$b->test(); // Outputs "b::test called"$b->func(); // Outputs "a::func called"$c->test(); // Outputs "b::test called"$d->test(); // Outputs "b::test called"

From outside the scope of a class, its methods

are called using the indirection operator ->

Page 11: Introduction to php oop

$this keyword

class myClass {

function myFunction() {

echo "You called myClass::myFunction";

}

function callMyFunction() {

// ???

}

}

Suppose we have a scenario where one of the function inside a class

require to call another function inside the same class

Page 12: Introduction to php oop

$this ketword

class myClass {

function myFunction($data) {

echo "The value is $data";

}

function callMyFunction($data) {

$this->myFunction($data);

}

}

$obj = new myClass();

$obj->callMyFunction(123); //This will output The value is 123.

We can acheive it by using a special keyword called “$this”

Page 13: Introduction to php oop

Constructors

• The constructor and destructor are special class methods that are called, as

their names suggest, on object creation and destruction, respectively.

• We can define a constructor in two ways

1. By defining a method with same name as of the class name

2. By defining a __construct() Magic Method

Page 14: Introduction to php oop

Constructor having same name of class

class foo {

function foo()

{

// PHP 4 style constructor

}

}

new foo();

Page 15: Introduction to php oop

Constructor having same name of class

class foo {

function foo()

{

// PHP 4 style constructor

}

}

new foo();

This style of constructors were used in

PHP4 but deprecated by php 5 .Moreover

this method has several draw backs—for

example, if you decided to rename your

class, you would also have to rename your

constructor.

Page 16: Introduction to php oop

Constructor using Magic Methods

class foo {

function __construct()

{

echo “constructor”;

}

}

new foo();

Page 17: Introduction to php oop

Constructor using Magic Methods

class foo {

function __construct()

{

echo “constructor”;

}

}

new foo();

• Magic methods are special methods in php

which will be invoked by PHP itself upon

certain scenarios.magic methods usually starts

with “__”

•__construct is a magic method which will

be invoked by PHP whenever an object of

that class is created.

•If PHP couldnt find __construct method it

will look for the old style php constructor ie

a method that has same name as class

Page 18: Introduction to php oop

Destructors

• Destructors are called right before an object is destroyed, and is

useful for performing cleanup procedures—such as disconnecting

from a remote resource, or deleting temporary files

class foo {

function __destruct()

{

echo “destructor”;

}

}

$a=new foo();

unset($a); //Destructor will be called

Page 19: Introduction to php oop

Visibility

• public : The resource can be accessed from any scope.

• protected : The resource can only be accessed from within the

class where it is defined and its descendants.

• private : The resource can only be accessed from within the

class where it is defined.

• Final : The resource is accessible from any scope, but

cannot be overridden in descendant classes. This

only applies to methods and classes and not to

properties

Page 20: Introduction to php oop

class foo {

public $foo = ’bar’;

protected $baz = ’bat’;

private $qux = ’bingo’;

function __construct()

{

var_dump(get_object_vars($this));

}

}

class bar extends foo {

function __construct()

{

var_dump(get_object_vars($this));

}

}

class baz {function __construct() {$foo = new foo();

var_dump(get_object_vars($foo));

}}new foo();new bar();new baz();

Page 21: Introduction to php oop

class foo {

public $foo = ’bar’;

protected $baz = ’bat’;

private $qux = ’bingo’;

function __construct()

{

var_dump(get_object_vars($this));

}

}

class bar extends foo {

function __construct()

{

var_dump(get_object_vars($this));

}

}

class baz {function __construct() {$foo = new foo();

var_dump(get_object_vars($foo));

}}new foo();new bar();new baz();

get_object_vars() is a special method which accepts object as argument and

returns an array of member properties inside that object

Page 22: Introduction to php oop

class foo {

public $foo = ’bar’;

protected $baz = ’bat’;

private $qux = ’bingo’;

function __construct()

{

var_dump(get_object_vars($this));

}

}

class bar extends foo {

function __construct()

{

var_dump(get_object_vars($this));

}

}

class baz {function __construct() {$foo = new foo();

var_dump(get_object_vars($foo));

}}new foo();new bar();new baz();

array(3) {["foo"]=>string(3) "bar“["baz"]=>string(3) "bat"["qux"]=>string(5) “bingo"}

Page 23: Introduction to php oop

class foo {

public $foo = ’bar’;

protected $baz = ’bat’;

private $qux = ’bingo’;

function __construct()

{

var_dump(get_object_vars($this));

}

}

class bar extends foo {

function __construct()

{

var_dump(get_object_vars($this));

}

}

class baz {function __construct() {$foo = new foo();

var_dump(get_object_vars($foo));

}}new foo();new bar();new baz();

array(2) {["foo"]=>string(3) "bar"["baz"]=>string(3) "bat"}

Page 24: Introduction to php oop

class foo {

public $foo = ’bar’;

protected $baz = ’bat’;

private $qux = ’bingo’;

function __construct()

{

var_dump(get_object_vars($this));

}

}

class bar extends foo {

function __construct()

{

var_dump(get_object_vars($this));

}

}

class baz {function __construct() {$foo = new foo();

var_dump(get_object_vars($foo));

}}new foo();new bar();new baz();

array(1) {["foo"]=> string(3) "bar"}

Page 25: Introduction to php oop

Static Methods and Properties

• Unlike regular methods and properties, static methods and properties are

accessible as part of a class itself, as opposed to existing only within the

scope of one of its instances.

• Ie there is no need to create objects for accessing the static methods and

properties. It can be accessed by using the class name itself

Page 26: Introduction to php oop

Static Methods and Properties

class foo {

static $bar = "bat";

static public function baz()

{

echo "Hello World";

}

}

$a= new foo();

$a>baz();

echo $foo->bar;

foo::bazNotice: Undefined property: foo::$bar in PHPDocument1 on line 17

Page 27: Introduction to php oop

Static Methods and Properties

class foo {

static $bar = "bat";

static public function baz()

{

echo "Hello World";

}

}

$a= new foo();

$a->baz();

echo $a->bar;

Will cause an error as belowa::bazNotice: Undefined property: a::$bar in PHPDocument1 on line 17

Page 28: Introduction to php oop

Static Methods and Properties

class foo {

static $bar = "bat";

static public function baz()

{

echo "Hello World";

}

}

$a= new foo();

foo->baz();

echo foo->bar;

//Will display as belowHello Worldbat

Page 29: Introduction to php oop

Class Constants

• Class constants work in the same way as regular constants, except

they are scoped within a class. Class constants are public, and

accessible from all scopes; for example, the following script will

output Hello World:

class foo {

const BAR = "Hello World";

}

echo foo::BAR;

Page 30: Introduction to php oop

Abstract Classes

• An abstract class essentially defines the basic skeleton of a specific

type of encapsulated entity—for example, you can use an abstract

class to define the basic concept of “car” as having two doors, a

lock and a method that locks or unlocks the doors.

• Abstract classes cannot be used directly, but they must be

extended so that the descendent class provides a full complement

of methods

Page 31: Introduction to php oop

Abstract Classes

abstract class DataStore_Adapter {

private $id;

abstract function insert();

abstract function update();

public function save()

{

if (!is_null($this->id)) {

$this->update();

} else {

$this->insert();

}

}

}

class PDO_DataStore_Adapter extendsDataStore_Adapter {

public __construct($dsn){

// class’s own method definition

}function insert(){

// definition of abstract method

}function update(){

// definition of abstract method

}}

Page 32: Introduction to php oop

Interfaces

• Interfaces, on the other hand, are used to specify an API that a

class must implement.

• This allows you to create a common “contract” that your classes

must implement in order to satisfy certain logical requirements

Page 33: Introduction to php oop

Interfaces

interface DataStore_Adapter {

public function insert();

public function update();

public function save();

public function newRecord($name = null);

}

class PDO_DataStore_Adapter implementsDataStore_Adapter {

public function insert(){// ...}public function update(){// ...}public function save(){// ...}public function newRecord($name = null){}

}

Page 34: Introduction to php oop

Determining An Object’s Class

• It is often convenient to be able to determine whether a given object is an

instance of a particular class, or whether it implements a specific interface.

This can be done by using the instanceof operator:

if ($obj instanceof MyClass) {

echo "\$obj is an instance of MyClass";

}

• Naturally, instanceof allows you to inspect all of the ancestor classes of your

object, as well as any interfaces.

Page 35: Introduction to php oop

Questions?

“A good question deserve a good grade…”

Page 36: Introduction to php oop

End of day

Page 37: Introduction to php oop

If this presentation helped you, please visit our page facebook.com/baabtra and like it.

Thanks in advance.

www.baabtra.com | www.massbaab.com |www.baabte.com

Page 38: Introduction to php oop

Contact Us

Emarald Mall (Big Bazar Building)Mavoor Road, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550

NC Complex, Near Bus StandMukkam, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550

Start up VillageEranakulam,Kerala, India.

Email: [email protected]