[OOP]: Object-oriented programming: Load husband - Override


Posted on 2015-08-18 10:00:37


1. Load husband (Overloading) The declaration of a class with the same name multiple methods but different parameters (different data types, different number of parameters) declared husband called method (method overloading). Or briefly on each standard as below so that you understand.

  • The method is the same 1 class, with the same name together but have different argument list is called overloaded methods.
  • Constructors can also be overloaded.
  • Depending on how I call arguments that it would call the corresponding function.
  • Overloads are polymorphic forms (polymorphism) in the compilation (compile time).

For example: Overloading methods used do ?, we consider small exercise: Calculate the square area, the area of a rectangle, the area of a triangle. ( I'll give you a hint of self nhé code )

  • We will create three methods are called Area of ​​that, if such
  • There is one parameter passed, it will mean to the area of ​​a square
  • 2 parameter passed, it understood that the area of ​​a rectangle
  • There are 3 parameter passed, it understood that the area of ​​a triangle.

2. Override (Override) In PHP, if a programmer to create a method in the class have the same name as a method was created in the parent class is called overwrite method (Method Overriding). Or a more understandable manner as follows.

  • As Method appeared in the superclass and appear in person in the subclass.
  • When the object of the class calls the method will select and run by the method in the subclass.
  • If the class does not have a new method to identify in which the superclass to run
  • Override is polymorphic forms (polymorphism) in the execution (Runtime)

For example, you copy the code below into a file pase php and run offline.

So in this post I was introduced to the basic concepts you two very important and in  object-oriented programming in PHP in general and in particular. Good luck!