950 likes | 1.04k Views
Object Oriented PHP. Jan 21st, 2004 Presented at CIT by Sarose. Note. You must use PHP version 5 or above to use all the features discussed in the slides. Class. A "class" is simply a prototype, that defines the variables and the methods common to all objects of a certain kind.
E N D
Object Oriented PHP Jan 21st, 2004 Presented at CIT by Sarose
Note • You must use PHP version 5 or above to use all the features discussed in the slides.
Class A "class" is simply a prototype, that defines the variables and the methods common to all objects of a certain kind..
What is Object? • Object is a programming structure that encapsulates data and functionality as a single unit..
An instance of a class is an object Did you notice Encapsulation?
Declaring Class class File { private $path; private $mode; public function File() { /* … */ } public function Open($filepath, $filename) { /* … */ } }
An Object $fp= new File( ); $fp is an instance of class File.
Default Constructor During object creation a class constructor is executed by default <?phpclass foo {function foo( ) { /* … */ } function foo($param) { /* ... */ } }?>
Constructor a new way <?phpclass newfoo {function __construct( ) { /* ... */ } }?>
Multiple constructors class newfoo { function __construct() { /* ... */} function __construct($param) { /* ... */ } function __construct($t, $c, $q=30) { /* ... */ } } $obj = new newfoo(); $obj = new newfoo(23,34,34); $obj = new newfoo(2,3); // is this ok?
destructor • Like java, PHP has no destructor. Although you can define destructor class newfoo { function __construct($param) { /* ... */ }function __destruct() { /* ... */ } }
destructors class MyDestructableClass { function __construct() { print "In constructor\n"; } function __destruct() { print "Destroying " . $this->name . "\n"; } }
Create an object • Use newkeyword to create a new object. class Foo { public $i=3; function test() { /* … */ } } $obj = new Foo( );
Calling an Object's Methods • To call a method, you append the method name to an object reference, with an intervening sign -> Like, $obj = new Foo(); $obj->test();
Variable Access Rules The direct manipulation of an object's variables by other objects and classes is discouraged because it's possible to set the variables to values that don't make sense. For example $obj = new Foo; $obj->i = 10; // direct access…Not good!
Variable access • Ideally, instead of allowing direct manipulation of variables, a class would provide methods through which other objects can get or set variables. like in next slide
methods access variables class MyClass { private $str = "Hello, World!\n"; function getStr() { return $this->str; } function setStr($val) { $this->str = $val;} /* ….*/ } $obj = new MyClass; $obj->setStr(“PHP5!”); echo $obj->getStr();
$this $this is a reference to the same instance that called the method. class SimpleClass { // member declaration public $var = 'a default value'; // method declaration public function displayVar() { echo $this->var; } }
Class visibility • public method/variable can be accessed from outside the class. • privateonly the same class can access private methods or variables. • protectedonly the method of the same class or derived classes can access protected methods or variables.
class foo { private $x; public $k; protected $j; publicfunction public_foo() { print("I'm public"); } protectedfunction protect_foo() { $this->private_foo(); } privatefunction private_foo() { $this->x = 3; } } class foo2 extends foo { public function play() { $this->protect_foo(); $this->public_foo(); $this->private_foo();} } $x = new foo(); $x->public_foo(); //error, why? $x->protect_foo(); $x->private_foo();
inheritance • Through the use of inheritance, programmers can reuse the code. • Advantage of reuse are enormous: faster and development time, easier maintenance, and simpler extensibility.
Multiple Inheritance • Unlike Java, PHP supports Multiple inheritance. class Telephone { /* … */ } class Fax { /* … */ } class FaxMachine extends Telephone, Fax {/* … */}
Access parent class SimpleClass { public function displayVar() { echo “parent var”; } /* … */ } class ExtendClass extends SimpleClass { function displayVar() { echo "Extending class\n"; parent::displayVar(); } }
Why Interface? • Capturing similarities among unrelated classes • Declaring methods that one or more classes are expected to implement. • Revealing an object's programming interface without revealing its class.
example • Remote control is an interface between you and a television set • English language is an interface between two people
interface Music { public function play(); } class Guitar implements Music { public function play () { echo “playing guitar”; } } class Flute implements Music { public function play() { echo “playing flute”; } }
Interfaces inherits • Like class Interface can inherits from multiple interface interface IUnknown { /* … */ } interface Point extends IUnknown { /*… */ } interface Polygon extends IUnknown { /* … */ }
Note • use interfaces when you see that something in your class will change frequently. See next examples
Problem • Suppose you are developing a MediaPlayer, which plays certain formats like Mp3, Ra. If you want to add a new format without changing the main code of MediaPlayer how will you accomplish that?
define a std. interface Pluginformat solution interface IPluginFormat { public function play($file); /* …. */ } function playStream (IPluginFormat p, $file) { p->play($file); }
implement MediaPlayer Interface with different music format classes class Mp3 implements IPluginFormat { public playStream($file) { echo “Mp3 player”; } } class Avi implements IPluginFormat { public playStream($file) { echo “AVI player”; } } class Midi implements IPluginFormat { public playStream($file) { echo “Midi player”; } }
playStream (Mp3 p, “test.mp3”);playStream (Avi a, “test.avi”);playStream (Midi p, “test.midi”);
Suppose, we want to add some new formats class Au implements IPluginFormat{ public function playStream ($file) { /* … *. } } class Ogg implements IPluginFormat { public function playStream ($file) { /* … *. } }
playStream (Mp3 p, “test.mp3”);playStream (Avi a, “test.avi”);playStream (Midi p, “test.midi”);playStream (Ogg q, “test.ogg”);playStream (Au r, “test.au”);
We don’t need to change the main code i.e playMusic function, Why? • What roles does IPluginFormat interface play?
abstract An abstract method only declares the method's signature and does not provide an Implementation. Remember Pure Virtual Class in C++. abstractclass AbstractClass { abstract public function test(); }
abstract class AbstractClass { abstract public function test(); } class ImplementedClass extends AbstractClass { publicfunction test() { echo "ImplementedClass::test() called.\n"; } } $o = new ImplementedClass( ); $o->test();
Abstract allows default behavior abstract class AbstractClass { public $test; abstract public function test(); public function ok() { echo "test"; } } function ok() is a default behavior. It is not an abstract method.
Interface vs Abstract • Abstract classes let you define some behaviors; they force your subclasses to provide others. • Abstract method should be defined explicitly as abstract.
What is type hint? PHP 5 introduces the ability to declare class type hints to the expected class of objects. interface Foo { function a(Foo $foo); } class Foo1 { function Foo1(Foo $k) { } }
Instanceof `Instanceof` is an operator that determines the type of the class. class Foo { /* … */ } class Foo1 { /* … */ } $foo = new Foo1(); if($foo instanceof Foo) { echo '$foo is a Foo object<br />'; } If($foo instanceof Foo1) { echo '$foo is a Foo object<br />'; }
final • "final" keyword to declare final members variable and methods. • “final” variable cannot change after it has been initialized.
Final method class Foo { final public function bar() { // ... } }
Will this work? class NewFoo extends Foo { function bar() { // ... } } No, NewFoo cannot override the bar function because bar is finalized method in Foo