Table of Contents
PHP类中的七种语法说明
类中的七种语法说明
-属性
-静态属性
-方法
-静态方法
-类常量
-构造函数
-析构函数
<code class="php hljs "><!--?php class Student { // 类里的属性、方法和函数的访问权限有 (函数和方法是同一个概念) // private 私有的 protected 受保护的 public 公有的 // 类常量 没有访问权限修饰符 const STUDENT = 'Tom'; // 属性 public $stu_name; // 静态属性 public static $stu_num = 1; // 方法 public function stuFunction() { echo 'non_static_function','<br/>'; } // 静态方法 public static function static_stuFunction() { echo 'static_function',' '; } // 构造函数 创建对象时自动调用 public function __construct($stu_name) { $this->stu_name = $stu_name; echo '__construct',' '; } // 析构函数 销毁对象时自动调用 public function __destruct() { echo '__destruct',' '; } } // 实例化类对象 $object = new Student('Tom'); // 对象调用属性 echo $object->stu_name,' '; // 对象调用静态属性 echo $object::$stu_num,' '; // 类调用静态属性 echo Student::$stu_num,' '; // 使用对象分别调用方法和静态方法 $object->stuFunction(); $object->static_stuFunction(); $object::stuFunction(); $object::static_stuFunction(); // 使用类分别调用方法和静态方法 Student::stuFunction(); Student::static_stuFunction(); // 类调用类常量 echo Student::STUDENT,' ';</code>
Copy after login
总结:
对象可以调用属性和静态属性,类只能调用静态属性。
对象可以调用方法和静态方法,类可以调用方法和静态方法。
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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article
Assassin's Creed Shadows: Seashell Riddle Solution
4 weeks ago
By DDD
What's New in Windows 11 KB5054979 & How to Fix Update Issues
3 weeks ago
By DDD
Where to find the Crane Control Keycard in Atomfall
4 weeks ago
By DDD
Roblox: Dead Rails - How To Complete Every Challenge
1 months ago
By DDD
Atomfall guide: item locations, quest guides, and tips
1 months ago
By DDD

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)
