Table of Contents
类中的七种语法说明
总结:
对象可以调用属性和静态属性,类只能调用静态属性。
对象可以调用方法和静态方法,类可以调用方法和静态方法。
Home php教程 php手册 PHP类中的七种语法说明

PHP类中的七种语法说明

Jun 21, 2016 am 08:46 AM

类中的七种语法说明

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

<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/>&#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>
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

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

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)