Main object oriented features (from php5 power programming)

7 downloads 522 Views 52KB Size Report
PHP. Main object oriented features. (from php5 power programming). Page 2. OO Model. • A OOP is a collection of objects. • An object has properties .... Example. • Managing password in a database. • Design a class for adding user and.
PHP Main object oriented features (from php5 power programming)

OO Model • A OOP is a collection of objects • An object has properties – State (private data)

• An object has methods • Every object is an instance of a class

Main characteristic • public/private/protected access modifiers for methods and properties. • Allows the use of common OO access modifiers to control access to methods and properties

class MyClass { private $id = 18; public function getId() { return $this->id; } }

Constructor • Unified constructor name • __construct() • ..or define a function with the same name of the class – Like in PHP4

class MyClass { function __construct() { print "Inside constructor"; } }

Example (1/2) class Person { private $name; function setName($name) { $this->name = $name; } function getName() { return $this->name; } }; $judy = new Person(); $judy->setName("Judy"); $joe = new Person(); $joe->setName("Joe"); print $judy->getName() . "\n"; //print Judy print $joe->getName(). "\n"; //print Joe

Example (2/2) class Person { function __construct($name) { $this->name = $name; } function getName() { return $this->name; } private $name; };

$judy = new Person("Judy"); $joe = new Person("Joe"); print $judy->getName(); print $joe->getName();

Same as before _construct is called by new It may have parameters It cannot return a value

Destructor • __destruct() • Called when an object is destroyed (no more reference)

class MyClass { function __destruct() { print "An object of type MyClass is being destroyed\n"; } } $obj = new MyClass(); $obj = NULL;

An object of type MyClass is being destroyed

Access protection of member variables class MyDbConnectionClass { public $queryResult; protected $dbHostname = "localhost"; private $connectionHandle; // ... }

class MyFooDotComDbConnectionClass extends MyDbConnectionClass { protected $dbHostname = "foo.com"; }

Static properties class MyUniqueIdClass {

self: refer to the current class

static $idCounter = 0; public $uniqueId; function __construct() { self::$idCounter++; $this->uniqueId = self::$idCounter; } }

$obj1 = new MyUniqueIdClass(); print $obj1->uniqueId . "\n"; //print 1 $obj2 = new MyUniqueIdClass(); print $obj2->uniqueId . "\n"; //print 2

Cloning objects class MyClass { public $var = 1; } obj1

object

obj2

obj1

obj2

$obj1 = new MyClass(); $obj2 = $obj1; $obj2->var = 2; print $obj1->var; //print 2

object

object

$obj1 = new MyClass(); $obj2 = clone $obj1; $obj2->var = 2; print $obj1->var; //print 1

POLYMORPHISM • Single class inheritance – like Java

• Multiple interface implementations – Final keyword class Child extends Parent { ... }

class A implements B, C, ... { ... }

interface I1 extends I2, I3, ... { ... }

Autoload • When writing object-oriented code, it is often customary to put each class in its own source file. • Using the _autoload() function (one per application) a class can be included at runtime

general.inc

file has same name of the class

Myclass.php

must be placed in /classes directory

autoload called if MyClass is not defined

main.php

Reflection • Allows to have class information at runtime • Just an example

… - Constants [0] { } - Static properties [0] { } - Static methods [0] { } - Properties [0] { } - Methods [1] { Method [ public method F ] …

Example • Managing password in a database • Design a class for adding user and checking user identity

Example: create a table

CREATE TABLE users ( email VARCHAR(128) NOT NULL PRIMARY KEY, passwd CHAR(40) NOT NULL );