PHP Object-Oriented Programming Tutorial-PHP source code

Source: Internet
Author: User
Ec (2); & lt ;? Php tutorial * & nbsp; Class Declaration & nbsp; * & nbsp; 1. what are you developing? Are you sure you want to write the classes & nbsp; * & nbsp; 2. the members in the class must belong to this class & nbsp; * & nbsp; [modifier keywords] class name {& script ec (2); script

/* Class declaration
* 1. What are you developing and are you sure you want to write?
* 2. The members in the class must belong to this class.
* [Modifier class keywords] class name {
* Member attributes:
* Member method:
*}
* 3. When declaring a member attribute in a class, there must be a modifier first. When you are not sure which word to use, use var or public
* Only one class is saved in a file. The file name contains the class name. File: class Name. class. php
* Write the Class Name:
* Variable: aaaBbbCcc
* Function: aaaBbbCcc
* Constant: AAABBBCCC
* Class Name: AaaBbbCcc
* 4. Member attributes in the class. If multiple objects have different attribute values when they are created, do not directly give the initial values.
*
*
* Instantiate objects through classes
* 1. Create an object with the new class name, which is the object of the created class.
* $ Object reference = new class name;
* 2. If there is a new keyword that is used to create an object, creating an object is to allocate a space in the memory.
*
* Only objects are stored in the same bucket.
*
* Object functions
*
* Allocate objects in memory
*
* Object usage
* Members of an object must be accessed through object references.
* Object> Member
*
* Object> member attributes
* Object> member Method
*
*
*
*/

---------------------------------------------------

Class Something {
// In the OOP class, the first character is usually uppercase.
Var $ x;
Function setX ($ v ){
// The method starts with lowercase words, and then uses uppercase letters to separate words, such as getValueOfArea ()
$ This-> x = $ v;
}
Function getX (){
Return $ this-> x;
}
}
?> ---------------------------------------------------


Of course, you can define it according to your preferences, but it is better to maintain a standard to make it more effective.

Data members are defined using the "var" declaration in the class. They have no type before assigning values to data members. A data member can
It is an integer, an array, an associated array, or an object.

The method is defined as a function in the class. When using a member variable of the class in the method, you should use $ this-> name. Otherwise
It can only be a local variable.

Use the new operator to create an object:

 

$ Obj = new Something;

Then you can use the member function to pass:

$ Obj-> setX (5 );
$ See = $ obj-> getX ();

In this example, the setX member function assigns 5 to the object's member variable x (not a class), and then getX returns its value 5.

You can access data members by referencing classes like $ obj-> x = 6. This is not a good OOP habit. I strongly recommend
Methods To access member variables. If you think of the member variables as unprocessable and use the methods only through the object handle, you will be
A Good OOP programmer. Unfortunately, PHP does not support declaring private member variables, so bad code is also allowed in PHP.

Inheritance is easy to implement in PHP, as long as the extend keyword is used.

-----------------------------------------------------

Class Another extends Something {
Var $ y;
Function setY ($ v ){
$ This-> y = $ v;
}
Function getY (){
Return $ this-> y;
}
}

?> ---------------------------------------------------

// Class declaration (telephone class)
Class Phone {
// Declare attributes
Var $ pinPai;
Var $ color;
Var $ batteryCapacity;
Var $ screenSize;

// Member Method
Function call (){

}

Function message (){

}

Function playMusic (){

}

Function photo (){

}
}

// Class instantiation

Class Something {
Var $ x;

Function Something ($ y ){
$ This-> x = $ y;
}

Function setX ($ v ){
$ This-> x = $ v;
}

Function getX (){
Return $ this-> x;
}
}

?> ---------------------------------------------------


Therefore, you can create an object:

 

$ Obj = new Something (6 );

The constructor automatically assigns value 6 to the data variable x. Constructors and methods are common PHP functions, so you can use the default parameters.

  

Function Something ($ x = "3", $ y = "5 ")

Next:

  

$ Obj = new Something (); // x = 3 and y = 5
$ Obj = new Something (8); // x = 8 and y = 5
$ Obj = new Something (8, 9); // x = 8 and y = 9

The default parameter uses the C ++ method, so you cannot ignore the value of Y. Instead, assign a value to X from left to right. If
If the input parameter is less than the required parameter, the default parameter is used.

When an object of A derived class is created, only its constructor is called, and the constructor of the parent class is not called.
Class constructor, you must display the call in the constructor of the derived class. This can be done because all methods of the parent class in the derived class have
Is available.

-----------------------------------------------------

Function Another (){
$ This-> y = 5;
$ This-> Something ();
// Display the call base class Constructor
}

?> ---------------------------------------------------

Class Person {

Var $ name;
Var $ age;
Var $ sex;

Function say (){

}

Function eat (){

}

Function run (){

}
}
// Instantiate
$ P1 = new Person;
$ P2 = new Person;
$ P3 = new Person;

$ Obj = new Classfoo ();
$ Str = serialize ($ obj );

// Save $ str to disk


// A few months later


// Input str from the disk

$ Obj2 = unserialize ($ str)

?> ---------------------------------------------------
You have recovered the member data, but do not include methods (as described in this document ). As a result, you can only use $ obj2-> x to access
Member variable (you have no other methods !) So do not try it at home.

// Access Object Member
$ P1-> name = "zhangsan ";
Echo $ p1-> name;
?>

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.