PHP object-oriented programming method example

Source: Internet
Author: User
This article mainly introduces the PHP object-oriented programming method, this paper analyzes in detail the concepts, definitions, constructor, destructor, inheritance, overloading, interfaces, abstract classes, and other concepts and usage skills of classes involved in php object-oriented programming with examples, for more information about PHP object-oriented programming, see the example in this article. We will share this with you for your reference. The details are as follows:

PHP5 began to support object-oriented, for example:

 ";}// Destructor function _ destruct () {echo'
Destruct ';} // function oper1 () {echo 'oper1
';} Function oper2 ($ param) {$ this-> attr1 = $ param; echo $ this-> attr1;} protected function oper3 () {echo 'this is protected function
';} // Do not inherit from final function oper5 () {} function _ get ($ name) {return $ this-> $ name;} function _ set ($ name, $ value) {$ this-> $ name = $ value;} // static function double ($ param) {return $ param * $ param ;}} $ a = new classname ('first'); $ B = new classname ('second'); $ c = new classname (); $ c-> oper2 ("hello"); echo'
'; Echo $ c-> attr1; echo'

'; Echo' Per-Class constant classname: PI-'. classname: PI; echo'
Static method: classname: double (3)-'. classname: double (3); echo'
'; // Implement inherited echo' implement inherited
'; Class B extends classname {function oper4 () {$ this-> oper3 (); // The protected method can only be used in} function oper1 () {// reload echo 'this is class B/'s oper1.
';}}$ D = new B ("forth"); $ d-> oper1 (); $ d-> oper4 (); // interface Displayable {function display (); function show ();} class C implements Displayable {function display () {echo 'this is the method of the corresponding interface.
';} Function show () {}}$ e = new C (); $ e-> display (); echo' check whether $ e is a C instance :'; echo ($ e instanceof C )? 'Yes': 'no'; // clone object $ f = clone $ e; echo'

You can use the _ clone () method to call '; // abstract class E {}// $ f = new E () when using the clone keyword (); // This row reports an error. the abstract class cannot be instantiated. // The parameter is overloaded. the multi-state class F {public $ a = 1; public $ B = 2; public $ c = 3; function displayString ($ elem) {echo'
String: '. $ elem;} function displayInt ($ elem) {echo'
Int :'. $ elem;} // note that the $ p parameter is passed as an array and must be used as a subscript to access function _ call ($ method, $ p) {if ($ method = 'display') {if (is_string ($ p [0]) {$ this-> displayString ($ p [0]);} else {$ this-> displayInt ($ p [0]) ;}}$ g = new F (); $ g-> display ('ABC '); // iterator that reads all attributes of the instance foreach ($ g as $ att) {echo'
'. $ Att;} // reflection echo'
'; $ Class = new ReflectionClass ('F'); echo'
';echo $class;echo '
';?>

I hope this article will help you with PHP programming.

For more details about PHP object-oriented programming method examples, refer to PHP Chinese network!

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.