Introduction to some advanced surface object programming features of PHP

Source: Internet
Author: User
This article briefly introduces some advanced surface object programming features of PHP, including interfaces and abstract classes that are frequently seen in Java and other OOP languages. For more information, see

This article briefly introduces some advanced surface object programming features of PHP, including interfaces and abstract classes that are frequently seen in Java and other OOP languages. For more information, see

In general, to learn PHP, you need to understand the following features:

Object cloning.One of the major improvements to the OOP model in PHP5 is to regard all objects as references rather than values. However, if all objects are considered as references, how can we create a copy of an object? The answer is to clone an object.

<? Phpclass Corporate_Drone {private $ employeeid; private $ tiecolor; function setEmployeeID ($ employeeid) {$ this-> employeeid = $ employeeid;} function getEmployeeID () {return $ this-> employeeid;} function setTiecolor ($ tiecolor) {$ this-> tiecolor = $ tiecolor;} function getTiecolor () {return $ this-> tiecolor ;}} $ drone1 = new Corporate_Drone (); $ drone1-> setemployee ID ("12345"); $ drone1-> setTiecolor ("red"); $ drone2 = clone $ drone1; $ drone2-> setEmployeeID ("67890"); printf ("drone1 employeeID: % d
", $ Drone1-> getEmployeeID (); printf (" drone1 tie color: % s
", $ Drone1-> getTiecolor (); printf (" drone2 employeeID: % d
", $ Drone2-> getEmployeeID (); printf (" drone2 tie color: % s
", $ Drone2-> getTiecolor ();?>

Inheritance.As described above, constructing a class hierarchy through inheritance is a key concept of OOP.

Class Employee {...} class Executive extends Employee {...} class CEO extends Executive {...}

Interface. An interface is a set of unimplemented method definitions and constants, which is equivalent to a type blueprint. The interface only defines what the class can do, without the implementation details. This chapter describes the interfaces supported by PHP5 and provides some examples to demonstrate this powerful OOP feature.

<? Phpinterface IPillage {// CONST 1; function emptyBankAccount (); function burnDocuments ();} class Employee {} class Excutive extends Employee implements IPillage {private $ totalStockOptions; function emptyBankAccount () {echo "Call CFO and ask to transfer funds to Swiss bank account";} function burnDocuments () {echo "Torch the office suite. ";}} class test {function testIP (IPillage $ ib) {echo $ ib- > EmptyBankAccount () ;}}$ excutive = new Excutive (); $ test = new test (); echo $ test-> testIP ($ excutive);?>

Abstract class. Abstract classes are actually classes that cannot be instantiated. An abstract class is inherited by an instantiated class, which is called a concreate class ). Abstract classes can be fully implemented, partially implemented, or not implemented at all.

Abstract class Class_name {// insert attribute definitions here // insert method definitions here}

Namespace. Namespaces can be divided into various libraries and classes based on the context to help you manage the code library more effectively.

<? Phpnamespace Library; class Clean {function printClean () {echo "Clean..." ;}}?> <? Phpinclude "test. php"; $ clean = new \ Library \ Clean (); $ clean-> printClean ();?>

If you have used other object-oriented languages, you may wonder why the above features do not include some OOP features that are familiar to other languages? The reason is simple. PHP does not support these features. To keep you confused, the following lists the advanced OOP features not supported by PHP.

Method overload. PHP does not support polymorphism through function overloading. According to the Zend website, it may never be supported. For more information, see

Operator overload. Currently, operators cannot be given new meanings based on the modified data type. According to the discussion on the zend website, it is unlikely that this feature will be implemented in the future.

Multiple inheritance. PHP does not support multiple inheritance. However, multiple interfaces are supported.

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.