A complete PHP class contains seven syntax descriptions

Source: Internet
Author: User

A complete PHP class contains seven syntax descriptions

This article mainly introduces seven complete syntax descriptions of PHP classes, including attributes, static attributes, methods, static methods, class constants, constructors, and destructor, this article provides sample code and detailed Annotations to help you quickly understand how to write classes. For more information, see

Class Seven syntax descriptions

-Attributes

-Static attributes

-Method

-Static Method

-Class Constants

-Constructor

-Destructor

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

<? Php

Class Student {

// You have the permission to access attributes, methods, and functions in the class (functions and methods are the same concept)

// Private protected public

// The class constant does not have an access modifier

Const STUDENT = 'Tom ';

// Attributes

Public $ stu_name;

// Static attributes

Public static $ stu_num = 1;

// Method

Public function stuFunction (){

Echo 'non _ static_function ',' <br/> ';

}

// Static method

Public static function static_stuFunction (){

Echo 'static _ function', '<br/> ';

}

// Automatically called when the constructor creates an object

Public function _ construct ($ stu_name ){

$ This-> stu_name = $ stu_name;

Echo '_ construct', '<br/> ';

 

}

// The Destructor is automatically called when an object is destroyed.

Public function _ destruct (){

Echo '_ destruct', '<br/> ';

}

}

 

// Instantiate a Class Object

$ Object = new Student ('Tom ');

// Object call attributes

Echo $ object-> stu_name, '<br/> ';

// Static attributes of object calls

Echo $ object: $ stu_num, '<br/> ';

// Class call static attributes

Echo Student: $ stu_num, '<br/> ';

// Call methods and static methods respectively using objects

$ Object-> stuFunction ();

$ Object-> static_stuFunction ();

$ Object: stuFunction ();

$ Object: static_stuFunction ();

// Use classes to call methods and static methods respectively

Student: stuFunction ();

Student: static_stuFunction ();

// Class call class constant

Echo Student: STUDENT, '<br/> ';

Summary:

Objects can call attributes and static attributes. classes can only call static attributes.

Objects can call methods and static methods. classes can call methods and static methods.

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.