PHP Object-Oriented programming--basic practice Day 2

Source: Internet
Author: User
The Orientation in PHP ObjectPractice
Basic Practice
Advanced practices
Special Practice
The concept of a class
The concept of instantiation
constructor function
Destructors
Data access
ObjectThe concept of references
The concept of a class
ObjectProgramming-Basic Practice Day 2 ">
Class
? A flock of birds with similar characteristics ObjectGrouped into a class
? class defines these similar ObjectThe same properties and methods that you have
? Class is similar ObjectThe description, called the definition of the class, is the class ObjectBlueprints or prototypes.
? Class of ObjectCalled an instance of a class (Instance)//class draws a frame, ObjectFill the frame
? The properties and methods of a class are collectively referred to as class members
Example
? An NBA player is a class definition.
? Examples of what Jordan, James, and Kobe Bryant call classes (Instance)
NBA players
+ Name
+ Height
+ Weight
+ Your team
+ Player Number
------------
+ Running ()
+ Jump ()
+ Dribble ()
+ Shooting ()
+ Slam Dunk ()
+ Pass ()
Instantiation of the Class (instantiate)

<插入两张图片>

Object Programming--Basic Practice Day 2 ">

Object Programming--Basic Practice Day 2 ">


Case
Instantiation cases for classes and classes
? How to define a class
? How to instantiate a class Object
? How to invoke a method of a class
? constructor function

? Destructors

 
  An instantiation of an object//class is instantiated as an object using the keyword new,new immediately after the name of this class and a pair of parentheses $jordan = new Nbaplayer ();//property members in an object can be Symbol to access the echo $jordan->name. " \ n ";///The member method in the object can access $jordan->dribble () by symbol, $jordan->pass (); >

Output:
Jordan
Dribbling
Passing
constructor function
 ObjectThe function __construct ($name, $height, $weight, $team, $playerNumber) {//is not explicitly called when instantiated, but is also called echo "in Nbaplayer constructor\n "; $this->name= $name; $this is a pseudo-variable in PHP that indicatesObjectTheir own. You can access it through the $this-> methodObjectThe properties and methods of $this->height= $height; $this->weight= $weight; $this->team= $team; $this->playernumber=$ Playernumber; }//Definition method Public Function run () {echo "running\n";} Public Function jump () {echo "jumping\n";} Public Function dribble () {echo "dribbling\n";} Public Function shoot () {echo "shooting\n";} Public Function Dunk () {echo "dunking\n";} Public Function Pass () {echo "passing\n";}} class toObjectInstantiation of the//class toObjectWhen using the keyword New,new followed by the name of this class and a pair of parentheses $jordan = new Nbaplayer ("Jordan", "198cm", "98kg", "Bull", "23");//ObjectThe property members in can access the Echo $jordan->name by using the symbols. \ n ";//ObjectThe member method in can access $jordan->dribble () by the symbol, $jordan->pass ();//each time with new instantiationObject, the constructor is called $james= new Nbaplayer ("James", "203cm", "120kg", "Heat", "6") with the parameter column following the class name, and the Echo $james->name;echo $james- >height;echo $james->weight;echo $james->team;echo $james->playernumber;? >

Output:
In Nbaplayer Constructor
Jordan
Dribbling
Passing
In Nbaplayer Constructor
James
203cm
120kg
Heat
6

Destructors

 ObjectThe function __construct ($name, $height, $weight, $team, $playerNumber) {//is not explicitly called when instantiated, but is also called echo "in Nbaplayer constructor\n "; $this->name= $name; $this is a pseudo-variable in PHP that indicatesObjectTheir own. You can access it through the $this-> methodObjectThe properties and methods of $this->height= $height; $this->weight= $weight; $this->team= $team; $this->playernumber=$ Playernumber; The}//destructor, which is called automatically at the end of the program execution, is usually used to clean up the resources used by the program, such as when the program uses a printer, and the printer resources can be freed from the destructor. function __destruct () {echo "destroying". $this->name. "
";} Define method Public Function run () {echo "running\n";} Public Function jump () {echo "jumping\n";} Public Function dribble () {echo "dribbling\n";} Public Function shoot () {echo "shooting\n";} Public Function Dunk () {echo "dunking\n";} Public Function Pass () {echo "passing\n";}} class toObjectInstantiation of the//class toObjectWhen using the keyword New,new followed by the name of this class and a pair of parentheses $jordan = new Nbaplayer ("Jordan", "198cm", "98kg", "Bull", "23");//ObjectThe property members in can access the Echo $jordan->name by using the symbols. \ n ";//ObjectThe member method in can access $jordan->dribble () by the symbol, $jordan->pass ();//each time with new instantiationObject, the constructor $james= new Nbaplayer ("James", "203cm", "120kg", "Heat", "6") is called with the parameter column after the class name, and the Echo $james->name;// By setting the variable to NULL, the call to the destructor can be triggered $james=null;echo "from now on James would not be used.
";? >

Output:
In Nbaplayer Constructor
Jordan
Dribbling
Passing
In Nbaplayer Constructor
James
Destroying James
From now on James would not be used.
Destroying Jordan

Basic concepts of object references
Object Reference Assignment
Object Programming--Basic Practice Day 2 ">
James is an object .
$james is the object 's reference, pointing directly at the object of James.
$james 1 and $james are two independent references, and a reference to this object $james 1 directly points to the object of James.
$james 2 is a reference to the object referenced by the $james object (a bit of a mouthful) and does not directly point to the object of James. $james 2 is by $james this object reference to the object of James.
There are now two references to objects that are directly directed to the object James, namely $james and $james1. And $james2 is the image of $james.
Example one:

 ObjectThe function __construct ($name, $height, $weight, $team, $playerNumber) {//is not explicitly called when instantiated, but is also called echo "in Nbaplayer constructor\n "; $this->name= $name; $this is a pseudo-variable in PHP that indicatesObjectTheir own. You can access it through the $this-> methodObjectThe properties and methods of $this->height= $height; $this->weight= $weight; $this->team= $team; $this->playernumber=$ Playernumber; The}//destructor, which is called automatically at the end of the program execution, is usually used to clean up the resources used by the program, such as when the program uses a printer, and the printer resources can be freed from the destructor. function __destruct () {echo "destroying". $this->name. "
";} Define method Public Function run () {echo "running\n";} Public Function jump () {echo "jumping\n";} Public Function dribble () {echo "dribbling\n";} Public Function shoot () {echo "shooting\n";} Public Function Dunk () {echo "dunking\n";} Public Function Pass () {echo "passing\n";}} class toObjectInstantiation of the//class toObjectWhen using the keyword New,new followed by the name of this class and a pair of parentheses $jordan = new Nbaplayer ("Jordan", "198cm", "98kg", "Bull", "23");//ObjectThe property members in can access the Echo $jordan->name by using the symbols. \ n ";//ObjectThe member method in can access $jordan->dribble () by the symbol, $jordan->pass ();//each time with new instantiationObject, the constructor $james= new Nbaplayer ("James", "203cm", "120kg", "Heat", "6") is called with the parameter column after the class name, and the Echo $james->name;// By setting the variable to NULL, you can trigger a destructor call//whenObjectWill not be used again when the destructor is triggered//james1 is also pointed to the new Nbaplayer (); $james 1= $james; $james =null;echo "From now on James would not be used.
";? >
Output:
In Nbaplayer Constructor
Jordan
Dribbling
Passing
In Nbaplayer Constructor
James
From now on James would not be used.
Destroying James
Destroying Jordan
Example two:
 ObjectThe function __construct ($name, $height, $weight, $team, $playerNumber) {//is not explicitly called when instantiated, but is also called echo "in Nbaplayer constructor\n "; $this->name= $name; $this is a pseudo-variable in PHP that indicatesObjectTheir own. You can access it through the $this-> methodObjectThe properties and methods of $this->height= $height; $this->weight= $weight; $this->team= $team; $this->playernumber=$ Playernumber; The}//destructor, which is called automatically at the end of the program execution, is usually used to clean up the resources used by the program, such as when the program uses a printer, and the printer resources can be freed from the destructor. function __destruct () {echo "destroying". $this->name. "
";} Define method Public Function run () {echo "running\n";} Public Function jump () {echo "jumping\n";} Public Function dribble () {echo "dribbling\n";} Public Function shoot () {echo "shooting\n";} Public Function Dunk () {echo "dunking\n";} Public Function Pass () {echo "passing\n";}} class toObjectInstantiation of the//class toObjectWhen using the keyword New,new followed by the name of this class and a pair of parentheses $jordan = new Nbaplayer ("Jordan", "198cm", "98kg", "Bull", "23");//ObjectThe property members in can access the Echo $jordan->name by using the symbols. \ n ";//ObjectThe member method in can access $jordan->dribble () by the symbol, $jordan->pass ();//each time with new instantiationObject, the constructor $james= new Nbaplayer ("James", "203cm", "120kg", "Heat", "6") is called with the parameter column after the class name, and the Echo $james->name;// By setting the variable to NULL, you can trigger a destructor call//whenObjectWill not be used again when the destructor is triggered//james1 is also pointed to the new Nbaplayer (); $james 1= $james; $james 1 Direct point to James $james2=&james; $james 2 is equivalent to the shadow of $james, pointing to $james, $james again point to James $james=null; Do not need to set $james2=null again, because they both effect is the same $james1=null; Any assignment of NULL equals the deletion of aObjectThe quote echo "from now on James would not be used."
";? >

Output:
In Nbaplayer Constructor
Jordan
Dribbling
Passing
In Nbaplayer Constructor
James
Destroying James
From now on James would not be used.
Destroying Jordan

The above describes the PHP object-oriented programming-the basic practice Day 2, including the aspects of the content, I hope to be interested in PHP tutorial friends helpful.

  • 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.