object oriented programming with php (2)
DESCRIPTION
Bayu Priyambadha, S.Kom. Object Oriented Programming with PHP (2). Topics. Final Keyword Class Abstraction Object Interfaces. Final Keyword. PHP introduces “Final” keyword, that prevent overriding of sub-class method “Final” can be implement at method and class - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Object Oriented Programming with PHP (2)](https://reader033.vdocuments.mx/reader033/viewer/2022050802/56815a6b550346895dc7c334/html5/thumbnails/1.jpg)
Bayu Priyambadha, S.Kom
![Page 2: Object Oriented Programming with PHP (2)](https://reader033.vdocuments.mx/reader033/viewer/2022050802/56815a6b550346895dc7c334/html5/thumbnails/2.jpg)
Final KeywordClass AbstractionObject Interfaces
![Page 3: Object Oriented Programming with PHP (2)](https://reader033.vdocuments.mx/reader033/viewer/2022050802/56815a6b550346895dc7c334/html5/thumbnails/3.jpg)
PHP introduces “Final” keyword, that prevent overriding of sub-class method
“Final” can be implement at method and class
Final class means that class cannot be extended
![Page 4: Object Oriented Programming with PHP (2)](https://reader033.vdocuments.mx/reader033/viewer/2022050802/56815a6b550346895dc7c334/html5/thumbnails/4.jpg)
<?phpclass BaseClass { public function test() { echo "BaseClass::test() called\n"; } final public function moreTesting() { echo "BaseClass::moreTesting() called\n"; }}
class ChildClass extends BaseClass { public function moreTesting() { echo "ChildClass::moreTesting() called\n"; }}
// Results in Fatal error: Cannot override final method BaseClass::moreTesting()?>
<?phpclass BaseClass { public function test() { echo "BaseClass::test() called\n"; } final public function moreTesting() { echo "BaseClass::moreTesting() called\n"; }}
class ChildClass extends BaseClass { public function moreTesting() { echo "ChildClass::moreTesting() called\n"; }}
// Results in Fatal error: Cannot override final method BaseClass::moreTesting()?>
![Page 5: Object Oriented Programming with PHP (2)](https://reader033.vdocuments.mx/reader033/viewer/2022050802/56815a6b550346895dc7c334/html5/thumbnails/5.jpg)
<?phpfinal class BaseClass { public function test() { echo "BaseClass::test() called\n"; }
// Here it doesn't matter if you specify the function as final or not final public function moreTesting() { echo "BaseClass::moreTesting() called\n"; }}
class ChildClass extends BaseClass {}// Results in Fatal error: Class ChildClass may not inherit from final class (BaseClass)?>
<?phpfinal class BaseClass { public function test() { echo "BaseClass::test() called\n"; }
// Here it doesn't matter if you specify the function as final or not final public function moreTesting() { echo "BaseClass::moreTesting() called\n"; }}
class ChildClass extends BaseClass {}// Results in Fatal error: Class ChildClass may not inherit from final class (BaseClass)?>
![Page 6: Object Oriented Programming with PHP (2)](https://reader033.vdocuments.mx/reader033/viewer/2022050802/56815a6b550346895dc7c334/html5/thumbnails/6.jpg)
Class that is defined as an abstract cannot be instantiated
Class that consist of one abstract method, must be declared as abstract class
![Page 7: Object Oriented Programming with PHP (2)](https://reader033.vdocuments.mx/reader033/viewer/2022050802/56815a6b550346895dc7c334/html5/thumbnails/7.jpg)
abstract class AbstractClass{ // Force Extending class to define this method abstract protected function getValue(); abstract protected function prefixValue($prefix);
// Common method public function printOut() { print $this->getValue() . "\n"; }}
class ConcreteClass1 extends AbstractClass{ protected function getValue() { return "ConcreteClass1"; }
public function prefixValue($prefix) { return "{$prefix}ConcreteClass1"; }}
abstract class AbstractClass{ // Force Extending class to define this method abstract protected function getValue(); abstract protected function prefixValue($prefix);
// Common method public function printOut() { print $this->getValue() . "\n"; }}
class ConcreteClass1 extends AbstractClass{ protected function getValue() { return "ConcreteClass1"; }
public function prefixValue($prefix) { return "{$prefix}ConcreteClass1"; }}
![Page 8: Object Oriented Programming with PHP (2)](https://reader033.vdocuments.mx/reader033/viewer/2022050802/56815a6b550346895dc7c334/html5/thumbnails/8.jpg)
Object Interfaces allow us to define method without detail
Methods of Interfaces must be define with public modifier
Using “implement” keyword to use object interfaces
![Page 9: Object Oriented Programming with PHP (2)](https://reader033.vdocuments.mx/reader033/viewer/2022050802/56815a6b550346895dc7c334/html5/thumbnails/9.jpg)
Class cannot implement more than one class that consist of the same method
Interface can be inherited as same as usual class using “extend” keyword
Class that implement the interfaces must be use all methods it owned, with the same specification
![Page 10: Object Oriented Programming with PHP (2)](https://reader033.vdocuments.mx/reader033/viewer/2022050802/56815a6b550346895dc7c334/html5/thumbnails/10.jpg)
<?phpinterface a{ public function foo();}
interface b extends a{ public function baz(Baz $baz);}
// This will workclass c implements b{ public function foo() { }
public function baz(Baz $baz) { }}?>
<?phpinterface a{ public function foo();}
interface b extends a{ public function baz(Baz $baz);}
// This will workclass c implements b{ public function foo() { }
public function baz(Baz $baz) { }}?>
![Page 11: Object Oriented Programming with PHP (2)](https://reader033.vdocuments.mx/reader033/viewer/2022050802/56815a6b550346895dc7c334/html5/thumbnails/11.jpg)
Monyet merupakan salah satu dari jenis Hewan, Monyet mempunyai kemampuan untuk Berjalan, Makan dan Bersuara, tapi suatu saat Monyet apabila dilatih akan bisa Berkendara seperti naik sepeda yang tidak dipunyai oleh hewan lainnya.
![Page 12: Object Oriented Programming with PHP (2)](https://reader033.vdocuments.mx/reader033/viewer/2022050802/56815a6b550346895dc7c334/html5/thumbnails/12.jpg)
<?phpecho “Terima
Kasih....!!!”?>