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

Source: Internet
Author: User
Programming | object | object-oriented programming for Project PHP: Methods for developing large PHP projects (III.)





Author: Luis Argerich translator: Limodou





overload (unlike overlay) is not supported in PHP. In OOP, you can overload a method to implement two or more methods with the same


's name, but there are different numbers or types of arguments (this depends on the language). PHP is a loosely typed language, so the type overload does not


works, however, overloading does not work by varying the number of parameters.





sometimes overloading constructors in OOP is great, so you can create objects in different ways (passing a different number of arguments). in PHP

The trick to implement it in
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;


}


}





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


uses this class to be 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 easy to use.





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


covers 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,


calls which draw method 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, OK, 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);


//Will invoke the Circle draw method





$board->nicedrawing ($obj 2);


//Will invoke the rectangle draw method





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





object-oriented programming with PHP


some "purists (purists)" may say that PHP is not a real object-oriented language, which is true. PHP is a mixed type


language, you can use OOP, or you can use traditional procedural programming. However, for large projects, you may want/need to use
in PHP

Pure OOP to declare classes, and only objects and classes 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 software engineering

The foundation of
. Applying these concepts to web-based projects is the key to success in future Web sites.











Transferred from Phpbuilder.com














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.