How to abstract a class

Source: Internet
Author: User

How to abstract a class

1. declaration of class;

2. Variables (member properties);

① format: There must be a modifier in front of the member property of the class, public/protected/private/static;

Modifier $ variable name [= default value];

When declaring a class by default, do not give the initial value first, because all objects that are created later have their own properties;

For example public $name = ' Gaoyuanyuan ';

Note: The member property cannot be an expression with an operator, a variable, a method, or a function call;

② Definition Method:

Public $varA = 100; Ordinary VALUES (4 scalars: integers, floating-point numbers, booleans, strings);

Public $varB = myconstant; Constant

Public $varC = self::classconstant; Static properties

Public $varD = Array (true,false); Array

3. Member methods (functions);

① format: [modifier]function function name (parameter 1, parameter 2,... ... ){

[function body];

return [returned value];

}

② modifier: public/protected/private/static/abstract/final

③ Note: A declared member method must be related to an object and cannot be a meaningless operation

Example: The following is a member method that declares several people, typically declaring a member method below a member property

Public function say () {//person can speak method

Echo ' people are talking '; function body

}

Public Function Run () {//How people can walk

Echo ' people are walking '; function body

}

Declaring a phone class

Class phone{     //Declare a phone class
Declaring 4 phone-related member properties
public $manufacturers; First member property, used to store the appearance of a phone
public $solor; Second member property to set the appearance color of the phone
Public $battery _capacity; Third member property to define the battery capacity of the phone
Public $screen _size; Fourth member property to define the screen size of the phone
  
The first member method is used to declare that the phone has the function of answering the call
Public Function call () {
Echo ' is on the phone '; //function body, can be the content of the call
}

The second member method is used to declare that the phone has the ability to send messages
Public function message () {
Echo ' is sending messages '; function body, which can be the specific content of sending information
}

The third member method is used to declare that the phone has the ability to take pictures
Public function photo () [
Echo ' is taking pictures '; function body, which can be the whole process of taking pictures
}
  
}

How to abstract a class

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.