object-oriented Programming in PHP: Ways to develop large PHP projects (III.)

Source: Internet
Author: User
Overloading (unlike overlay) is not supported in PHP. In OOP, you can overload a method to implement two or more methods with the same name, but with different numbers or types of arguments (which depends on the language). PHP is a loosely typed language, so it does not work through type overloading, but overloading does not work with different numbers of arguments.

Sometimes overloading constructors in OOP is great, so you can create objects in different ways (passing a different number of arguments). The trick to implementing it in PHP is:

--------------------------------------------------------------------------------
<?php

Class Myclass {
function Myclass () {
$name = "Myclass". Func_num_args ();
$this-> $name ();

Note that $this->name () is generally wrong, but here $name is the name of the method to be invoked
}
function Myclass1 ($x) {
Code
}
function Myclass2 ($x, $y) {
Code
}
}

?>--------------------------------------------------------------------------------
Using this class is transparent to the user through additional processing in the class:

$obj 1=new Myclass (' 1 '); Will call Myclass1

$obj 2=new Myclass (' 1 ', ' 2 '); Will call Myclass2

Sometimes this is very useful.

Polymorphic
Polymorphism is the ability of an object to decide which object to invoke, based on the parameters of the object passed at run time. For example
If you have a figure class, it defines a draw method. and derived the circle and rectangle classes, in the derived class you override
Covered the draw method, you might also have a function that expects to use a parameter X and can call $x->draw (). If you have polymorphism,
Which draw method is invoked depends on the type of object you pass to the function.

Polymorphism in an interpreted language like PHP (imagine a C + + compiler generating such code, which method should you call?) You
Also do not know what type of object you have, good, this is not the point is very easy and natural. So PHP certainly supports polymorphism.

--------------------------------------------------------------------------------
<?php

function nicedrawing ($x) {

Suppose this is a method of the Board class
$x->draw ();
}

$obj =new Circle (3,187);
$obj 2=new Rectangle (4,5);

$board->nicedrawing ($obj);
The draw method that will invoke circle

$board->nicedrawing ($obj 2);
The draw method that will invoke rectangle

?>--------------------------------------------------------------------------------

Object-oriented Programming with PHP
Some "purists" (purists) may say that PHP is not a true object-oriented language, which is true. PHP is a hybrid language, you can use OOP, or you can use traditional procedural programming. However, for large projects, you may want/need to use pure OOP in PHP to declare classes, and only objects and classes are used in your project.

As the project grows, using OOP can be helpful, and OOP code is easy to maintain, easy to understand and reuse. These are the foundations of software engineering. Applying these concepts to web-based projects is the key to success in future Web sites.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.