Php learning note class declaration and Object Instantiation

Source: Internet
Author: User

Copy codeThe Code is as follows: <? Php
/* 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 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 Person {
Var $ name;
Var $ age;
Var $ sex;
Function say (){
}
Function eat (){
}
Function run (){
}
}
// Instantiate
$ P1 = new Person;
$ P2 = new Person;
$ P3 = new Person;
// 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.