Php object-oriented programming

Source: Internet
Author: User

<? Php Tutorial
/* PHP object-oriented programming
 *
* PHP5
 *
* Array and object: both belong to the PHP compound type (one variable can store multiple units)
 *
* Objects are more powerful than arrays. They not only store multiple data, but also store functions in objects.
 *
* Three features of an object: encapsulation, inheritance, and polymorphism
 *
* Object-oriented programming (oop) // meets the reusability flexibility and scalability in software engineering
 *
* Differences between object-oriented and process-oriented
 * 
* Minimum unit: function // process-oriented
* Minimum unit: object // object-oriented
 *
* What is an object?
Recently, Sina's management began to cash in and open news revealed that the MBO of that year made it the top shareholder of Sina. Now, it has become the second biggest shareholder. The equity ratio is not much different: it is only 0.1% different from FMR9.24 %, the largest shareholder. However, the Sina management layer has an agreement on selling shares. If this part of the shares may be sold in the future, the management will only have 5.74%. Sina may indeed become an "unowned company" again ".

This is why Sina, as a company, has troubles in the future: the profit model is unclear and the possibility of being unmaster begins to show up again.
* An object is an object.
* Object:
* Member attributes = variables // defines the object's appearance and status
* Member method = function // define the object function
 *
* What is a class?
* Category, type, and custom type
 *   
* Define attributes and methods of this type in the class
 *
* A relationship between classes and objects
 *
* Declaration class --> instantiate an object (create an object) --> use an object
 *
 *
* The most rare thing is how to design forward-to-object programs, rather than object-oriented syntax.
 *
*/

?>

Original members:
Var $ name; // The name of the declarer
Var $ sex; // declare the gender of a person
Var $ age; // declare the person's age
Function run (){.......}
Changed to Encapsulation:
Private $ name; // encapsulate a person's name using the private keyword
Private $ sex; // encapsulate the gender of a person using the private keyword.
Private $ age; // encapsulate the age of a person using the private keyword.


Private function run (){......} // Encapsulate the human walking method with the private keyword
Note: as long as there are other keywords before the member attribute, the original keyword "var" should be removed ".
You can encapsulate a member (member attribute and member method) in private mode. The encapsulated members cannot be directly accessed outside the class, and only the objects can be accessed by themselves. The following code produces an error:
Class Person
{
// The following are the member attributes of a person.
Private $ name; // The name of a person, which is encapsulated by private
Private $ sex; // the gender of a person, which is encapsulated by private
Private $ age; // age of a person, which is encapsulated by private
// How this person can speak
Function say ()
{
Echo "My name is :". $ this-> name. "Gender :". $ this-> sex. "My age is :". $ this-> age. "<br> ";
}
// The way this person can walk is encapsulated by private
Private function run ()
{
Echo "this person is walking ";
}
}
// Instantiate a person's instance object
$ P1 = new Person ();
// Try to assign values to private attributes, and the result will be incorrect
$ P1-> name = "James ";
$ P1-> sex = "male ";
$ P1-> age = 20;
// An error occurs when you try to print a private property.
Echo $ p1-> name. "<br> ";
Echo $ p1-> sex. "<br> ";
Echo $ p1-> age. "<br>"
// Try to print the private member method, and the result will be incorrect
$ P1-> run ();
Output result:
Fatal error: Cannot access private property Person: $ name
Fatal error: Cannot access private property Person: $ sex
Fatal error: Cannot access private property Person: $ age
Fatal error: Cannot access private property Person: $ name
Fatal error: Call to private method Person: run () from context''

From the instance above, we can see that private members cannot be accessed externally, because private members can only access the object themselves, for example, $ p1: the object wants to describe its private attributes and access the private attributes in the say () method. (No access control is added. The default value is public and can be accessed anywhere)
// This person can talk about his/her own private attributes and access the private method here.

Function say ()
{
Echo "My name is :". $ this-> name. "Gender :". $ this-> sex. "My age is :". $ this-> age. "<br> ";
// You can also access private methods here
// $ This-> run ();
}

Because the Member method say () is public, we can call the say () method outside the class and change the above code;

Class Person
{
// The following are the member attributes of a person.
Private $ name; // The name of a person, which is encapsulated by private
Private $ sex; // the gender of a person, which is encapsulated by private
Private $ age; // age of a person, which is encapsulated by private
// Define a constructor parameter to assign values to the private attribute name $ name, gender $ sex, and age $ age.
Function _ construct ($ name, $ sex, $ age)
{
// Assign the initial value to the private member attribute $ this-> name through the $ name passed in by the constructor
$ This-> name = $ name;
// The $ sex passed in through the constructor is used to assign the initial enable value to the private member attribute $ this-> sex.
$ This-> sex = $ sex;
// Assign the initial value to the private member attribute $ this-> age through the $ age passed in by the constructor
$ This-> age = $ age;
}
// This person can talk about his/her own private attributes and access the private method here.
Function say ()
{
Echo "My name is :". $ this-> name. "Gender :". $ this-> sex. "My age is :". $ this-> age. "<br> ";
}
}
// Create three objects $ p1, p2, and $ p3 by using the constructor, and input three different real parameters: name, gender, and age.
$ P1 = new Person ("zhang san", "male", 20 );
$ P2 = new Person ("Li Si", "female", 30 );
$ P3 = new Person ("Wang Wu", "male", 40 );
// The following method of accessing the $ p1 object
$ P1-> say ();
// The following method of accessing the $ p2 object
$ P2-> say ();
// The following method is used to access the $ p3 object.

$ P3-> say ();
Output result:
My name is John. Gender: Male. My age is: 20.
My name is Li Si Gender: Female my age is: 30
My name is: Wang Wu Gender: Male my age is: 40

 

<? Php
Class user {
Private $ shell; // user identity: a two-dimensional array containing names and permissions
Private $ ob; // module object, for example, financial module caiwu
Function _ construct ($ shell ){
$ This-> shell = $ shell;
   }
Function mannger ($ ob ){
$ This-> ob = new $ ob ($ this-> shell );
   }
Function qianguize (){
$ This-> shell [name]! = 'Beebot '? Die ('! '):'';
$ This-> ob = new mklove ($ this-> shell );
   }
}
Class caiwu {
Function _ construct ($ shell ){
If (! In_array ('caiwu', $ shell [quanxian]) {
Echo ($ shell [name]. "You cannot manage finance ~! <Br/> ");
} Else {
Echo $ shell [name]. "you can manage finance <br/> ";
    }
 }
}
Class mklove {
Function _ construct (& $ shell ){
If (! In_array ('mklove', $ shell [quanxian]) die ('you can't ');
Echo "www.111cn.net ";
$ Shell [quanxian] [] = 'caiwu ';
 }
}
 
/*
User is a user class.
Caiwu!
*/
$ Shell = array (name => 'Fs', quanxian => array ('caiwu', 'xingzheng', 'cailiao', 'mklove '));
$ Shell0 = array (name => 'F', quanxian => array ('cailiao', 'mklove '));

 

$ Jingli = new user ($ shell );
$ Xiaomi = new user ($ shell0 );
 
 
Echo "$ Jingli-> mannger ('caiwu ');
$ Xiaomi-> mannger ('caiwu ');
 

Echo "$ Xiaomi-> qianguize ();
$ Xiaomi-> mannger ('caiwu ');
?>

 

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.