A complete PHP class contains seven kinds of syntax descriptions

Source: Internet
Author: User
Tags php class

This article mainly introduces a complete PHP class contains seven kinds of syntax description, the syntax includes attributes, static properties, methods, static methods, class constants, constructors, destructors, and this article gives you a quick understanding of how classes are written, and the friends you need can refer to

Seven syntax descriptions in a class

-Properties

-Static properties

-Method

-Static method

-Class constants

-Constructors

-destructor

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 This is the ; > <?php class Student {//Class properties, methods, and functions have access rights (functions and methods are the same concept)//private proprietary protected protected public publicly owned//class constants No access modifier CO NST STUDENT = ' Tom '; Property public $stu _name; static property public static $stu _num = 1; Method Public Function Stufunction () {echo ' non_static_function ', ' <br/> ';}//static method public static function static_s Tufunction () {echo ' static_function ', ' <br/> ';}//constructor automatically invokes public function __construct ($stu _name) {$this-& when creating objects Gt;stu_name = $stu _name; Echo ' __construct ', ' <br/> ';  }//destructors automatically invoke public function __destruct () {echo ' __destruct ', ' <br/> ';}} when destroying objects  //Instantiation class Object $object = new Student (' Tom '); Object Invocation Properties Echo $object->stu_name, ' <br/> '; Object invoke the Static property echo $object:: $stu _num, ' <br/> '; ClassInvoke Static properties Echo Student:: $stu _num, ' <br/> '; Use objects to invoke methods and static methods individually $object->stufunction (); $object->static_stufunction (); $object:: Stufunction (); $object:: Static_stufunction (); Use classes to invoke the method and static method Student::stufunction () respectively; Student::static_stufunction (); class to invoke class constants Echo Student::student, ' <br/> ';

Summarize:

Objects can invoke properties and static properties, and classes can invoke only static properties.

objects can call methods and static methods, and 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.