码迷,mamicode.com
首页 > Web开发 > 详细

php 中 self 和 static 的区别

时间:2020-03-11 10:30:59      阅读:51      评论:0      收藏:0      [点我收藏+]

标签:cti   的区别   class   public   elf   his   区别   function   this   

php 中 self 和 static 的区别


class Foo
{
    public static $str = 'This is foo';

    public static function show()
    {
        echo __METHOD__ . PHP_EOL;
        echo static::$str;
    }
}

class Boo extends Foo
{
    public static $str = 'This is boo';
}

Boo::show();

# 输出结果
# Foo::show
# This is boo

使用 static 调用的是当前类的变量

class Foo
{
    public static $str = 'This is foo';

    public static function show()
    {
        echo __METHOD__ . PHP_EOL;
        echo self::$str;
    }
}

class Boo extends Foo
{
    public static $str = 'This is boo';
}

Boo::show();

# 输出结果
# Foo::show
# This is foo

使用 self 调用的是最高级的父类的变量

php 中 self 和 static 的区别

标签:cti   的区别   class   public   elf   his   区别   function   this   

原文地址:https://www.cnblogs.com/pandaLIU/p/12460671.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!