Blogger Information
Blog 61
fans 0
comment 0
visits 53923
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
作用域解析符
笑颜常开的博客
Original
1048 people have browsed it

<?php
//范围解析符(作用域解析符)
class Book{
//    动态成员
   public $name='我是廖广';
//    静态成员
   protected static $author='Peter Zhu';
//    类常量,不要设置访问限制
   const PRICE=90;
//    动态方法,可访问动态成员和静态成员
   public function getInfo1(){
//    普通动态成员
//        return $this->name;
//        静态成员
//        尽管可以在普通方法中访问静态成员,但是推荐尽可能不用这样做
       return self::$author;
   }
//    静态方法,属于类的,必须要用类来调用,可以被所有该类的对象所共享
   public static function getInfo2(){
//    普通动态成员,不能访问
//        return $this->name;
//        静态成员
       return self::$author.self::PRICE;
   }
}
//子类Study继承自Book
class Study extends Book{
   public static function getInfo3()
   {
//        应当使用当前父类的引用标识符:parent
//        return parent::$author; // TODO: Change the autogenerated stub
//        return Book::$author; // TODO: Change the autogenerated stub
       return static::$author; // TODO: Change the autogenerated stub
   }
}
$book=new Book();
echo $book->name.'<br>';
//echo $book->getInfo1();
echo $book->getInfo2();
echo '<hr>';
$study=new Study();
echo $study->getInfo3();

Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post