search
Homephp教程php手册PHP类中的七种语法说明

类中的七种语法说明

-属性
-静态属性
-方法
-静态方法
-类常量
-构造函数
-析构函数

<code class="language-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/>&#39;;
        }
        // 静态方法
        public static function static_stuFunction() {
            echo &#39;static_function&#39;,&#39;
&#39;;
        }
        // 构造函数 创建对象时自动调用
        public function __construct($stu_name) {
            $this->stu_name = $stu_name;
            echo &#39;__construct&#39;,&#39;
&#39;;

        }
        // 析构函数 销毁对象时自动调用
        public function __destruct() {
            echo &#39;__destruct&#39;,&#39;
&#39;;
        }
    }

    // 实例化类对象
    $object = new Student(&#39;Tom&#39;);
    // 对象调用属性
    echo $object->stu_name,&#39;
&#39;;
    // 对象调用静态属性
    echo $object::$stu_num,&#39;
&#39;;
    // 类调用静态属性
    echo Student::$stu_num,&#39;
&#39;;
    // 使用对象分别调用方法和静态方法
    $object->stuFunction();
    $object->static_stuFunction();
    $object::stuFunction();
    $object::static_stuFunction();
    // 使用类分别调用方法和静态方法
    Student::stuFunction();
    Student::static_stuFunction();
    // 类调用类常量
    echo Student::STUDENT,&#39;
&#39;;</code>

总结:

对象可以调用属性和静态属性,类只能调用静态属性。

对象可以调用方法和静态方法,类可以调用方法和静态方法。



Statement
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

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software