PHP Introductory tutorials for object-oriented feature analysis (inheritance, polymorphism, interface, abstract class, abstract methods, etc.) _php tips

Source: Internet
Author: User

This example describes the object-oriented nature of PHP. Share to everyone for your reference, specific as follows:

demo1.php

<?php
  header (' content-type:text/html; charset=utf-8; ');
  Create a Computer class
  Computer {
    //what is called within the class, is to create the scope within the curly braces of the class called within the class, other places outside the class.
    //public is the public of the field, which can be accessed outside of the field class, and the assignment and value of the
    $_name = ' Lenovo ';
  $computer = new computer ();
  $computer-> _name = ' Dell ';
  echo $computer->_name;
? >

demo2.php

<?php
  header (' content-type:text/html; charset=utf-8; ');
  Class Computer {
    //private is privatized, that is, the operation of the encapsulation of the field, inaccessible outside the class, value and assignment can not operate
    private $_name = ' Lenovo ';
  }
  $computer = new computer ();
  echo $computer->_name;
? >

demo3.php

<?php
  header (' content-type:text/html; charset=utf-8; ');
  Class Computer {
    private $_name = ' Lenovo ';
    This time I use a public approach to accessing private fields
    //Because private fields can only be accessed within a class, and the external public method is within the class.
    //More public methods are public, so they are accessible outside the class.
    the Public Function _run () {
      //field must be a class-> field when invoked within a class, and $_name is just a generic variable.
      //Field calls outside the class is the Object-> field, and the class must use the Computer-> _name
      //But in this class, you can use a keyword instead of the word to replace Computer, which is $this
      echo $this->_name;
    }
  }
  $computer = new computer ();
  $computer-> _run ();
? >

demo4.php

<?php
  header (' content-type:text/html; charset=utf-8; ');
  Class Computer {
    private $name;
    Private $model;
    Private $cpu;
    Private $keyboard;
    Private $show;
    Private $ZB;
    You must write an external entry to access the public
    function GetName () {return
      $this->name;
    }
    You must write an internal entry, assign the private field to the public
    function SetName ($name) {//
      the $name here is just a variable, it's a parameter
      //$this->name Is the field of the class
      $this->name = $name;
    }
  }
  $computer = new computer ();
  echo $computer->getname ();
  $computer->setname (' Dell ');
  echo $computer->getname ();
? >

demo5.php

<?php
  header (' content-type:text/html; charset=utf-8; ');
  Class Computer {
    private $_name;
    Private $_model;
    Private $_cpu;
    When an object outside of a class calls a private field directly, it checks to see if there is an interceptor,
    //If the $_name is assigned directly, then the __set method intercepts, and no error is made. //
    Use Interceptor for assignment and value
    //Assignment
    Private function __set ($_key,$_value) {
      //use $_key = ' _name ', then $_value = ' Lenovo '; c12/>//$this->_name = ' Association ';
      $this->$_key = $_value;
    }
    Value
    Private Function __get ($_key) {return
      $this-> $_key;
      If $_key = ' _name ' then $this-> _name;
      If $_key = ' _cpu ' then $this-> _cpu;
      If $_key = ' _model ' then $this-> _model
    }
  }
  $computer = new computer ();
  $computer->_name = ' Association ';
  $computer->_cpu = ' quad core ';
  $computer->_model = ' i7 ';
  echo $computer->_name;
  echo $computer->_cpu;
  echo $computer->_model;
? >

demo6.php

<?php
  header (' content-type:text/html; charset=utf-8; ');
  Class Computer {
    private $_name;
    Private $_model;
    Private $_cpu;
    The __set and __get methods are private or can be executed because the
    current program pointer is already in the class. Within a class, you can execute a
    private method within the encapsulated method//class without any errors.
    //It only needs to be intercepted indirectly. Interception is performed in the inner class.
    //Plainly, __set () and __get () are built-in PHP methods with certain specificity of the
    private function __set ($_key, $_value) {
      $this->$_key = $ _value;
    }
    Private Function __get ($_key) {return
      $this->$_key;
    }
  }
  $computer = new computer ();
  $computer->_name = ' Association ';
  $computer->_cpu = ' quad core ';
  $computer->_model = ' i7 ';
  echo $computer->_name;
  echo $computer->_cpu;
  echo $computer->_model;
? >

demo7.php

<?php
  header (' content-type:text/html; charset=utf-8; ');
  Class Computer {
    Const NAME = ' DELL ';
  }
  Output method Class for constants:: Constant
  echo computer::name;    DELL
?>

demo8.php

<?php
  header (' content-type:text/html; charset=utf-8; ');
  Class Computer {public
    $_count = 0;
    Public Function _add () {
      $this-> _count++;  $_count = $_count+1 $_count++
    }
  //Make a cumulative effect
  $computer 1 = new Computer ();
  $computer 1->_add ();
  $computer 1->_add ();
  $computer 1->_add ();
  echo $computer 1-> _count;
  echo ' <br/> ';
  $computer 2 = new Computer ();
  $computer 2->_add ();
  $computer 2->_add ();
  $computer 2->_add ();
  echo $computer 2-> _count;
? >

demo9.php

<?php
  header (' content-type:text/html; charset=utf-8; ');
  Class Computer {public
    static $_count = 0;
    Public Function _add () {
      //If it is a static member field, it should be invoked with self instead of $this
      self::$_count++
    }
  }
  Make a cumulative effect
  $computer 1 = new Computer ();
  $computer 1->_add ();
  echo Computer::$_count;
  $computer 1->_add ();
  echo Computer::$_count;
  $computer 1->_add ();
  echo Computer::$_count;
  echo ' <br/> ';
  $computer 2 = new Computer ();
  $computer 2->_add ();
  echo Computer::$_count;
  $computer 2->_add ();
  echo Computer::$_count;
  $computer 2->_add ();
  echo computer::$_count;
? >

demo10.php

<?php
  header (' content-type:text/html; charset=utf-8; ');
  Class Computer {public
    static $_count = 0;
    public static function _add () {
      self::$_count++
    }
  }
  Computer::_add ();
  Computer::_add ();
  Computer::_add ();
  echo computer::$_count;
? >

demo11.php

<?php
  header (' content-type:text/html; charset=utf-8; ');
  Class Computer {
  }
  $computer = new Computer ();
  echo $computer instanceof computer;
? >

demo12.php

<?php
  header (' content-type:text/html; charset=utf-8; ');
  This is the parent class, computer class
  Computer {public
    $_name = ' Lenovo ';
    Public Function _run () {
      echo ' Lenovo is running! '
    }
  }
  Subclass class, Notebook class
  Notecomputer extends Computer {
  }
  $noteComputer = new Notecomputer ();
  echo $noteComputer-> _name;
  $noteComputer-> _run ();
? >

demo13.php

<?php
  header (' content-type:text/html; charset=utf-8; ');
  Class Computer {public
    $_name = ' Lenovo ';
    Public Function _run () {
      echo ' Lenovo is running! '
    }
  }
  Class Notecomputer extends Computer {
    //I do not need the fields and methods of the parent class, you can override the method to overwrite the fields and methods of the parent class public
    $_name = ' Dell ';
    Public Function _run () {
      echo ' Dell is running! ';
    }
  }
  $noteComputer = new Notecomputer ();
  echo $noteComputer-> _name;
  $noteComputer-> _run ();
? >

demo14.php

<?php
  header (' content-type:text/html; charset=utf-8; ');
  Class Computer {
    //privatization, but cannot inherit the quilt class, this time should use the protected modifier to encapsulate
    protected $_name = ' Association ';
    protected function _run () {return
      ' Lenovo is running! '
    }
  }
  Class Notecomputer extends Computer {public
    function GetTop () {
      echo $this->_name;
      echo $this->_run ();
    }
  $noteComputer = new Notecomputer ();
  $noteComputer-> GetTop ();
? >

demo15.php

<?php
  header (' content-type:text/html; charset=utf-8; ');
  Class Computer {public
    $_name = ' Lenovo ';
    Public Function _run () {return
      ' Lenovo is running! '
    }
  }
  Class Notecomputer extends Computer {
    //My subclass has overridden the fields and methods of the parent class,
    //But I'm going to
    call the fields and methods of the parent class. Public $_name = ' Dell ';
    Public Function _run () {
      echo ' Dell is running! ';
      Echo Parent:: _run ();
    }
  }
  $noteComputer = new Notecomputer ();
  echo $noteComputer-> _name;
  $noteComputer-> _run ();
  The Delldell is running! Lenovo is running!
? >

demo16.php

<?php
  header (' content-type:text/html; charset=utf-8; ');
  Final if added in front of the class, it means that this class cannot be inherited
//Final class Computer {
//}
  class Computer {
    //final If added to the method before, Represents the ability to override methods
    final public Function _run () {
    }
  }
  class Notecomputer extends Computer {
    public function _run () {
    }
  }
  $noteComputer = new Notecomputer ();
? >

demo17.php

<?php
  header (' content-type:text/html; charset=utf-8; ');
  Create an abstract class, as long as you add abstract before class and the abstract class
  ///abstract class cannot be instantiated, that is, to create an object
  //Only an abstract method within a class, then the class must be an abstract class and the class must precede
  Abstract class Computer {public
    $_name = ' Lenovo ';
    Creating an abstract method
    //abstract method in an abstract class can not implement the content of the method body abstract public
    function _run ();
    Can I create a normal method in the abstract class public
    function _run2 () {
      echo ' I am the normal method of the parent class ';
    }
  }
  Class cannot implement multiple inheritance, only single inheritance is supported.
  //Abstract class is for subclasses to inherit, implement a specification and resource sharing
  class Notecomputer extends Computer {
    //abstract class abstract method, subclass must rewrite, or it will be an error.
    the normal method in the//abstract class does not need to be overridden, and subclasses inherit directly from the public
    function _run () {
      echo ' I am a subclass of method ';
    }
  }
  $noteComputer = new Notecomputer ();
  $noteComputer-> _run ();
  $noteComputer-> _run2 ();
  echo $noteComputer-> _name;
? >

demo18.php

<?php
  /
   * Should I use an abstract class or an interface?
   * If you want to inherit the method specification of multiple interfaces, then use the interface well.
   * If you want to share a method body content, use an abstract class.
   * * *
  /header (' content-type:text/html; charset=utf-8; ');
  Create an interface
  //interface can not be instantiated
  //interface is to standardize the implementation of its subclasses, in order to achieve a unified purpose. You can also share data
  interface Computer {
    //member fields must be variable
    const NAME = ' member ';
    All the methods in the interface are abstract methods and cannot write the method body
    //and the abstract method of the interface does not need to write an abstract public
    function _run ();
    Public function _run2 ();
  }
  Interface Computer2 {public
    function _run3 ();
  }
  Subclass inherits the argument of the interface, called implementation, the interface can implement the
  class Notecomputer implements Computer,computer2 {public
    function _run () {
      Echo ' I rewrote run ';
    }
    Public Function _run3 () {
      echo ' I rewrote run3 ';
    }
    Public Function _run2 () {
      echo ' I rewrote run2 ';
    }
  }
  $noteComputer = new Notecomputer ();
  $noteComputer-> _run ();
  $noteComputer-> _run2 ();
  $noteComputer-> _run3 ();
  echo notecomputer::name;
  Interface:: Constant
  //echo computer::name;
? >

demo19.php

<?php header (' content-type:text/html; charset=utf-8; ');
  What is called polymorphism, literal meaning, multiple forms//An action is performed by different people, and produces different effects or effects, i.e. polymorphism.
  A person performs the same action through different states, forming different effects, or being called polymorphic.
  The gardener cuts the flowers//The barber cuts the barber//The haircut/the total cutting redundancy/person notebook run win7 boot up//person desktop run XP power-on//Create an interface to standardize the way to run   Interface Computer {public function version ();//This method indicates what computer public function work () is used; How does this computer run?//Create a notebook class implementation interface class Notecomputer implements Computer {public Function version () {echo '
    Notebook ';
    The Public function work () {echo ' can be portable to run Win7 ';
    }///Create a desktop class implementation interface class Desktopcomputer implements Computer {public Function version () {echo ' desktop ';
    Public function work () {echo ' runs XP on workstation ';
    }//Create a user class Person {//Create a method to accept the computer (laptop, also can be a desktop computer)//How to accept, send their objects in the OK.
      Public Function _run ($type) {echo ' this person's ';
      $type-> version ();
    $type->work (); }//Polymorphic principle, is the class are written, do not modify it,As long as the call parameter changes outside the class//and the final result is changed, then this is polymorphism.
  There is an interface, two classes, one is a notebook class, one is a desktop class//created a notebook $noteComputer = new Notecomputer ();
  Create a desktop $desktopComputer = new Desktopcomputer ();
  Create a human $person = new Person (); Use of computer $person-> _run ($noteComputer);

 This pass is called the passing of an object reference?>

More about PHP Interested readers can view the site topics: "PHP object-oriented Program Design Primer", "PHP basic Grammar Introductory Course", "PHP operation and operator Usage Summary", "PHP Network Programming Skills Summary", "PHP Array" operation Skills Encyclopedia, " Summary of PHP string usage, Introduction to PHP+MYSQL database operations, and a summary of PHP common database operations Tips

I hope this article will help you with the PHP program design.

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.