首页 > Web开发 > 详细

php (二)

时间:2018-11-06 17:23:01      阅读:149      评论:0      收藏:0      [点我收藏+]

  class

  new  

  PHP 命名空间  namespace

  构造方法

class Hello{
    /**
     * Hello constructor.
     * @param $age 年龄/int
     * @param $name 名字/srting
     */
    public function __construct($age,$name) // 构造方法
    {
        $this->_age = $age;
        $this->_name = $name;
    }
    public function getAge(){
        return $this->_age;
    }
    public function getName(){
        return $this->_name;
    }
    private $_age,$name;
}
$h = new Hello(10,‘我‘); // 创建实例
$h->sayHello();

--------------------------------------------------

成员方法与类方法

  static

class Hello{
    /**
     * Hello constructor.
     * @param $age 年龄/int
     * @param $name 名字/srting
     */
    public function __construct($age,$name) // 构造方法
    {
        $this->_age = $age;
        $this->_name = $name;

        Hello::$NUM++;

        if(Hello::$NUM>Hello::MAX_MAN_NUM){
            throw new Exception(‘不能‘);
        }
    }
    public function getAge(){
        return $this->_age;
    }
    public function getName(){
        return $this->_name;
    }
    private $_age,$name;
    private static $NUM = 0; // 静态变量
    const MAX_MAN_NUM = 200; //常量
    // 都是描述类的
    public static  function sayHello(){
        echo ‘类‘;
    }

}
$h = new Hello(10,‘我‘); // 创建实例
$h->sayHello();

 

 

---------------------------------------------

类的继承

  extends

  方法重写.可以修改方法内的功能

  

---------------------------------------------

时间和日期

  time()

date_default_timezone_set(‘Asia/Shanghai‘); // 设置时区 不然...
echo date(‘Y-m-d H:i:s‘,time());

 

php (二)

原文:https://www.cnblogs.com/mysterious-killer/p/9916392.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!