Home > Backend Development > PHP Tutorial > PHP study notes Class declaration and object instantiation_PHP tutorial

PHP study notes Class declaration and object instantiation_PHP tutorial

WBOY
Release: 2016-07-21 15:28:43
Original
832 people have browsed it

Copy code The code is as follows:

/* Class declaration
* 1. You want to develop What is it, determine what class to write
* 2. The members in the class must belong to this class
* [Keywords that modify the class] class class name {
* member attributes:
* member Method:
* }
* 3. When declaring member attributes in a class, there must be a modifier in front of it. When you are not sure which word to use, use var or public
* A file only saves one class. The file name contains the class name, file: classname.class.php
* Class name writing:
* Variable: aaaBbbCcc
* Function: aaaBbbCcc
* Constant: AAABBBCCC
* Class Name: AaaBbbCcc
* 4. For member attributes in a class, if you create multiple objects and each object has a different attribute value, do not give the initial value directly. Give the value after the object is created
*
*
* Instantiate objects through classes
* 1. Use new to create a new object, plus the class name, which class object is created
* $Object reference=new class name ;
* 2. As long as there is a new keyword, an object is created. Creating an object is allocating a space in the memory
*
* Only objects have storage space in the memory
*
* The role of objects
*
* Allocation of objects in memory
*
* Usage of objects
* Members in the object must be accessed through references to the object
* Object->Members
*
* Object->Member Properties
* Object->Member Methods
*
*
*
*/
/ /Declaration of class (phone class)
class Phone{
//Declare attributes
var $pinPai;
var $color;
var $batteryCapacity;
var $screenSize;
//Member method
function call(){
}
function message(){
}
function playMusic(){
}
function photo(){
}
}
//Instantiation of class
class Person{
var $name;
var $age;
var $sex;
function say( ){
}
function eat(){
}
function run(){
}
}
//Instantiation
$p1=new Person;
$p2=new Person;
$p3=new Person;
//Access the members of the object
$p1->name="zhangsan";
echo $p1-> name;
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/323522.htmlTechArticleCopy the code as follows: ?php /* Class declaration* 1. What do you want to develop? Make sure to write What class* 2. Members in the class must belong to this class* [Keywords that modify the class] class class name {...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template