PHP object-oriented concepts and examples

Source: Internet
Author: User
A simple article on PHP object-oriented concepts and examples can be used as a reference for anyone who needs it. Keywords and special variables, new, class, extends. All three of them know

A simple article on PHP object-oriented concepts and examples. if you need it, you can give a simple reference.

Keywords and special variables

New, class, extends. All three of them.

:, The range resolution operator (also known as Paamayim Nekudotayim) or, more simply, a colon, can be used to access static members, methods, and constants. it can also be used to override the members and methods in the class.

Parent and self. parent are the names of the base classes that the derived classes refer to in The extends declaration. This avoids the use of the base class names in multiple places.

$ This pseudo variable. $ this points to the current instance. $ this is not necessarily the object to which the method belongs. sometimes the code in Class A calls A static method in Class B. example of http://www.php.net/manual/zh/language.oop5.basic.php

Static keyword. if the declared class member or method is static, you can directly access it without instantiating the class. however, apart from static methods, you cannot access static members through an object. in static methods, $ this is not used. use self ::.

Final keywords can act on classes and functions, so that classes cannot be inherited and methods cannot be overwritten.

Attribute

It can be initialized, but the initialization value must be a constant. the const keyword is used before a constant. the constant value must be a fixed value. it cannot be the result of a variable, class attribute, or other operations (such as function call.

Constructor and Destructor

These two functions do not secretly call the response functions of the base class, which is different from the java constructor mechanism. to achieve this effect, the execution must be displayed. Exceptions cannot be thrown in the destructor.

Abstract class: the declared abstract class methods cannot contain specific implementations, and the abstract classes cannot be instantiated. it must be inherited before instantiating its subclass. in addition, the sub-class access control should be the same as the abstract class, or be more relaxed. an abstract class contains at least one abstract method.

Interface

Using interfaces, you can specify the methods that a class must implement, but you do not need to define the specific content of these methods.

All methods defined must be public and the method is empty.

A constant can be defined, but no attribute exists.

The implementation of the interface (implements) must implement all methods and multiple interfaces can be implemented (note that methods cannot be renamed ).

Interfaces can be inherited by other interfaces (extends)

The instance code is as follows:

  1. /*
  2. * Defines the User interface.
  3. * And subclass NormalUser, VipUser, InnerUser
  4. */
  5. // The User interface defines three abstract methods.
  6. Interface User {
  7. Public function getName ();
  8. Public function setName ($ _ name );
  9. Public function getDiscount ();
  10. }
  11. Abstract class AbstractUser implements User {
  12. Private $ name = ""; // name
  13. Protected $ discount = 0; // discount
  14. Protected $ grade = ""; // level
  15.  
  16. Public function _ construct ($ _ name ){
  17. $ This-> setName ($ _ name );
  18. }
  19. Public function getName (){
  20. Return $ this-> name;
  21. }
  22. Public function setName ($ _ name ){
  23. $ This-> name = $ _ name;
  24. }
  25. Public function getDiscount (){
  26. Return $ this-> discount;
  27. }
  28.  
  29. Public function getGrade (){
  30. Return $ this-> grade;
  31. }
  32. }
  33. Class NormalUser extends actuser {
  34. Protected $ discount = 1.0;
  35. Protected $ grade = "NormalUser ";
  36. }
  37. Class VipUser extends actuser {
  38. Protected $ discount = 0.8;
  39. Protected $ grade = "VipUser ";
  40. }
  41. Class InnerUser extends actuser {
  42. Protected $ discount = 0.7;
  43. Protected $ grade = "InnerUser ";
  44. }
  45. ?>

Product. php

The instance code is as follows:

  1. Include_once ("User. php ");
  2. Include_once ("Product. php ");
  3. // How much does it cost to buy a product?
  4. Class ProductSettle {
  5. Public static function finalPrice (User $ _ user, Product $ _ product, $ number = 1 ){
  6. $ Price = $ _ user-> getDiscount () * $ _ product-> getProductPrice () * $ number;
  7. Return $ price;
  8. }
  9. }
  10. ?>

The example below is implementation. you can analyze it by yourself.

The instance code is as follows:

  1. Include_once ("./class/User. php ");
  2. Include_once ("./class/Product. php ");
  3. Include_once ("./class/ProductSettle. php ");
  4. $ Number = 10;
  5. $ Book = new BookOnline ("design mode ");
  6.  
  7. $ User = new NormalUser ("Tom ");
  8. $ Price = ProductSettle: finalPrice ($ user, $ book, $ number );
  9. $ Str = "Hello, dear user". $ user-> getName ()."
    ";
  10. $ Str. = "your level is". $ user-> getGrade ().",
    ";
  11. $ Str. = "your discount is". $ user-> getDiscount ()."
    ";
  12. $ Str. = "buy $ number book". $ book-> getProductName ();
  13. $ Str. = "" price is $ price

    ";
  14. Echo $ str;
  15.  
  16. $ User = new vipUser ("Tom ");
  17. $ Price = ProductSettle: finalPrice ($ user, $ book, $ number );
  18. $ Str = "Hello, dear user". $ user-> getName ()."
    ";
  19. $ Str. = "your level is". $ user-> getGrade ().",
    ";
  20. $ Str. = "your discount is". $ user-> getDiscount ()."
    ";
  21. $ Str. = "buy $ number book". $ book-> getProductName ();
  22. $ Str. = "" price is $ price

    ";
  23. Echo $ str;
  24. $ User = new InnerUser ("Tom ");
  25. $ Price = ProductSettle: finalPrice ($ user, $ book, $ number );
  26. $ Str = "Hello, dear user". $ user-> getName ()."
    ";
  27. $ Str. = "your level is". $ user-> getGrade ().",
    ";
  28. $ Str. = "your discount is". $ user-> getDiscount ()."
    ";
  29. $ Str. = "buy $ number book". $ book-> getProductName ();
  30. $ Str. = "" price is $ price

    ";
  31. Echo $ str;
  32. ?>

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.