Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pemrograman Web Object Oriented Programming in PHP 5.

Similar presentations

Presentation on theme: "Pemrograman Web Object Oriented Programming in PHP 5."— Presentation transcript:

1 Pemrograman Web Object Oriented Programming in PHP 5

2 What is a function?  Conceptually, what does a function represent? …give the function something (arguments), it does something with them, and then returns a result… Action or Method

3 What is a class?  Conceptually, a class represents an object, with associated methods and variables

4 Class Definition Example

5 Class Defintion  Similar to defining a function..  The definition does not do anything by itself. It is a blueprint, or description, of an object. To do something, you need to use the class…

6 Class Usage nama = 'Susan'; echo $susan->nama. ' jika menyapa, berkata: '. $susan->menyapa(); ?>

7 Using attributes within the class..  If you need to use the class variables within any class actions/methods, use the special variable $this in the definition: class manusia { public $nama; public function menyapa() { echo $this->nama. ' bilang Halo!'; } } $susan = new manusia; $susan->nama = 'Susan'; $susan->menyapa();

8 Constructor methods  A constructor method is a function that is automatically executed when the class is first instantiated.  Create a constructor by including a function within the class definition with the __construct name.  Remember.. if the constructor requires arguments, they must be passed when it is instantiated!

9 Constructor Example nama = $nama; } public function menyapa() { echo $this->nama. ' bilang Halo!'; } } ?>

10 Constructor Example menyapa(); … ?> Output: Susan bilang Halo!

11 Class Scope  Like functions, each instantiated object has its own local scope. nama; // Joko echo $mahasiswi->nama; // Susan; ?>

12 Inheritance  The real power of using classes is the property of inheritance – creating a hierarchy of interlinked classes. manusia mahasiswadosen parent children

13 Inheritance  The child classes 'inherit' all the methods and variables of the parent class, and can add extra ones of their own.  e.g. the child classes mahasiswa inherits the variable nama and method menyapa from the manusia class, and can add extra ones…

14 Inheritance example nama = $nama; } public $tugas = 'belajar'; } $susan = new mahasiswa('Susan'); echo $susan->menyapa(). ' ketika sedang '. $susan->tugas; Output: Susan bilang Halo! ketika sedang belajar

15 Method Override  Mahasiswa selalu berkata 'Hei!' ketika menyapa. nama. ' bilang Hei!'; } $susan = new mahasiswa('Susan'); echo $susan->menyapa(); Output: Susan bilang Hei! ketika sedang belajar

16 Child Constructors?  If the child class possesses a constructor function, it is executed and any parent constructor is ignored.  If the child class does not have a constructor, the parent's constructor is executed.  If the child and parent does not have a constructor, the grandparent constructor is attempted…  … etc.

17 Objects within Objects  It is perfectly possible to include objects within another object nama = $nama; }

18 Objects within objects example baju = new pakaian; echo $susan->nama. ' memakai baju warna '. $susan->baju->warna; ?> Output: Susan memakai baju warna merah

19 Encapsulation  Encapsulation is a way of storing an object or data as a property within another object, so that the outer object has full control over what how the internal data or object can be accessed.  This, in combination with making the inner object/property private, enables information hiding.information hiding.

20 Encapsulation Example baju = new pakaian; $this->baju->warna = 'biru'; } public function warnaBaju() { return $this->baju->warna; } $susan = new manusia(); echo 'Susan memakai baju berwarna '. $susan->warnaBaju(); Output: Susan memakai baju berwarna biru

21 Abstract Class  It's a kind "father" that must be inherited to be used. Classes that inherit differ from them only in the abstract methods and can access the methods of the parent class using the keyword parent.  Features:  can not be instantiated  methods can be abstract (not implemented)  methods may be not abstract (implemented)  a class can inherit from a single abstract class

22 Interface  The clearest definition is that an interface is a contract.  Features:  All classes that implement an interface must develop all the methods that have been defined  The class implementing the interface must use the exact same method signatures as are defined in the interface. Not doing so will result in a fatal error  All methods declared in an interface must be public, this is the nature of an interface  A class can implement more than one interface  An interface can be used by the Type HintingType Hinting

23 Polymorphism  Polymorphism is the ability (in programming) to present the same interface for differing underlying forms (data types).

24 Polymorphism Example

25 Polymorphism Example bicara(); // Meong... $hewan = new anjing; $hewan->bicara(); // Guk... Guk... ?>

26 Final Keyword  PHP introduces "Final" keyword to prevent sub-class method overriding.  "Final" keyword can be implemented on properties, methods and classes  Final class means it cannot be inherited

27 Deleting objects  So far our objects have not been destroyed till the end of our scripts..  Like variables, it is possible to explicitly destroy an object using the unset() function.

28 A copy, or not a copy..  Entire objects can be passed as arguments to functions, and can use all methods/variables within the function.  Remember however.. like functions the object is COPIED when passed as an argument unless you specify the argument as a reference variable &$variable

Download ppt "Pemrograman Web Object Oriented Programming in PHP 5."

Similar presentations

Ads by Google