Home  >  Article  >  Backend Development  >  2个基础有关问题

2个基础有关问题

WBOY
WBOYOriginal
2016-06-13 13:30:08837browse

2个基础问题
两个问题:

(1)
类里头的常量是否允许实例双冒号的形式直接访问,张恩民同学不可以?实际情况如何?

class A{
  const mm=10;
  function __construct(){
  echo self::mm;
  }
}
echo $a::mm;
?>

(2)
静态成员,通过实例来调用,这样的方式好么?为何
class A{
  public static $mm=10;
}
$a=new A;
echo $a::$mm;
?>

------解决方案--------------------

PHP code

class B{
  const mm=10;
  function __construct(){
  echo self::mm;
  }
}
echo B::mm;
#10

class C {
  public static $mm=11;
}
$class_a=new C;
echo $class_a::$mm;
#11 <div class="clear">
                 
              
              
        
            </div>
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