1 / 94

Object Oriented PHP

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.

wood
Download Presentation

Object Oriented PHP

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Object Oriented PHP Jan 21st, 2004 Presented at CIT by Sarose

  2. Note • You must use PHP version 5 or above to use all the features discussed in the slides.

  3. Class A "class" is simply a prototype, that defines the variables and the methods common to all objects of a certain kind..

  4. What is Object? • Object is a programming structure that encapsulates data and functionality as a single unit..

  5. An instance of a class is an object Did you notice Encapsulation?

  6. Declaring Class class File { private $path; private $mode; public function File() { /* … */ } public function Open($filepath, $filename) { /* … */ } }

  7. An Object $fp= new File( ); $fp is an instance of class File.

  8. Default Constructor During object creation a class constructor is executed by default <?phpclass foo {function foo( ) { /* … */ } function foo($param) { /* ... */ }    }?>

  9. Constructor a new way <?phpclass newfoo {function __construct( ) { /* ... */ }             }?>

  10. 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?

  11. destructor • Like java, PHP has no destructor. Although you can define destructor     class newfoo {        function __construct($param) { /* ... */ }function __destruct() { /* ... */ }     }

  12. destructors class MyDestructableClass { function __construct() { print "In constructor\n"; } function __destruct() { print "Destroying " . $this->name . "\n"; } }

  13. Create an object • Use newkeyword to create a new object. class Foo { public $i=3; function test() { /* … */ } } $obj = new Foo( );

  14. 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();

  15. 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!

  16. 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

  17. 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();

  18. $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; } }

  19. 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.

  20. 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();

  21. Inheritance

  22. 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.

  23. inheritance

  24. Multiple Inheritance • Unlike Java, PHP supports Multiple inheritance. class Telephone { /* … */ } class Fax { /* … */ } class FaxMachine extends Telephone, Fax {/* … */}

  25. Access parent class SimpleClass { public function displayVar() { echo “parent var”; } /* … */ } class ExtendClass extends SimpleClass { function displayVar() { echo "Extending class\n"; parent::displayVar(); } }

  26. Interfaces

  27. 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.

  28. example • Remote control is an interface between you and a television set • English language is an interface between two people

  29. 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”; } }

  30. Interfaces inherits • Like class Interface can inherits from multiple interface interface IUnknown { /* … */ } interface Point extends IUnknown { /*… */ } interface Polygon extends IUnknown { /* … */ }

  31. Note • use interfaces when you see that something in your class will change frequently. See next examples

  32. 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?

  33. define a std. interface Pluginformat solution interface IPluginFormat { public function play($file); /* …. */ } function playStream (IPluginFormat p, $file) { p->play($file); }

  34. 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”; } }

  35. playStream (Mp3 p, “test.mp3”);playStream (Avi a, “test.avi”);playStream (Midi p, “test.midi”);

  36. Suppose, we want to add some new formats class Au implements IPluginFormat{ public function playStream ($file) { /* … *. } } class Ogg implements IPluginFormat { public function playStream ($file) { /* … *. } }

  37. 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”);

  38. We don’t need to change the main code i.e playMusic function, Why? • What roles does IPluginFormat interface play?

  39. Abstract class

  40. 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(); }

  41. abstract class AbstractClass { abstract public function test(); } class ImplementedClass extends AbstractClass { publicfunction test() { echo "ImplementedClass::test() called.\n"; } } $o = new ImplementedClass( ); $o->test();

  42. 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.

  43. 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.

  44. Class Type Hints

  45. 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) { } }

  46. 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 />'; }

  47. final

  48. final • "final" keyword to declare final members variable and methods. • “final” variable cannot change after it has been initialized.

  49. Final method class Foo { final public function bar() { // ... } }

  50. Will this work? class NewFoo extends Foo { function bar() { // ... } } No, NewFoo cannot override the bar function because bar is finalized method in Foo

More Related