Several PHP object-oriented small instances

Source: Internet
Author: User
Abstract class: Abstract classes cannot be instantiated. abstract classes are the same as other classes. variables and methods can be defined. abstract classes can also define an abstract method. abstract class methods are not executed, however, it may be executed in its derived class.

Abstract class: Abstract classes cannot be instantiated. abstract classes are the same as other classes. variables and methods can be defined. abstract classes can also define an abstract method. abstract class methods are not executed, however, it may be executed in its derived class.

Example 1: abstract class

  1. Abstract class foo {
  2. Protected $ x;
  3. Abstract function display ();
  4. Function setX ($ x ){
  5. $ This-> x = $ x;
  6. }
  7. }
  8. Class foo2 extends foo {
  9. Function display (){
  10. // Code
  11. }
  12. }
  13. ?>

_ Call:A special _ call () method is added to the PHP5 object. this method is used to monitor other methods in an object. If you try to call a method that does not exist in an object, the __call method will be automatically called.

Example 2 :__ call

  1. Class foo {
  2. Function _ call ($ name, $ arguments ){
  3. Print ("Did you call me? I'm $ name! ");
  4. }
  5. } $ X = new foo ();
  6. $ X-> doStuff ();
  7. $ X-> fancy_stuff ();
  8. ?>

This special method can be used to implement the "overload" action, so that you can check your parameters and pass parameters by calling a private method.

Example 3: Use _ call to implement "overload" action

  1. Class Magic {
  2. Function _ call ($ name, $ arguments ){
  3. If ($ name = 'Foo '){
  4. If (is_int ($ arguments [0]) $ this-> foo_for_int ($ arguments [0]);
  5. If (is_string ($ arguments [0]) $ this-> foo_for_string ($ arguments [0]);
  6. }
  7. } Private function foo_for_int ($ x ){
  8. Print ("oh an int! ");
  9. } Private function foo_for_string ($ x ){
  10. Print ("oh a string! ");
  11. }
  12. } $ X = new Magic ();
  13. $ X-> foo (3 );
  14. $ X-> foo ("3 ");
  15. ?>

_ Set and _ get

This is a great method. the __set and _ get methods can be used to capture variables and methods that do not exist in an object.

Example 4: _ set and _ get

  1. Class foo {
  2. Function _ set ($ name, $ val ){
  3. Print ("Hello, you tried to put $ val in $ name ");
  4. }
  5. Function _ get ($ name ){
  6. Print ("Hey you asked for $ name ");
  7. }
  8. }
  9. $ X = new foo ();
  10. $ X-> bar = 3;
  11. Print ($ x-> winky_winky );
  12. ?>

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.