zoukankan      html  css  js  c++  java
  • php 总结(4) 类 和命名空间 构造函数 时间日期

    1.类的创建方式和使用

         在构造函数外引用 一定$this  w我的属性

    <?php 
    
    class Dates{
    
          // 属性 
    	public $length;
    	public $offset;
    	public $pages; 
    	public $page;
    	public $show;
    	public $prev;
    	public $next;
    	function __construct($tot,$length){
    
    		$this->length=$length;
    		$this->page=$_GET['p']?$_GET['p']:1;
    		$this->pages=ceil($tot/$this->length);
    		$this->offset=($this->page-1)*$this->length;
    		$this->prev=$this->page-1;
    		$this->next=$this->page+1;
    		if ($this->prev<1) {
    			$previd='layui-btn-disabled';
    			$prev=1;
    		}
    		if ($this->next>$this->pages) {
    			$nextid='layui-btn-disabled';
    			$next=$pages;
    
    		}
    
    		$this->show="<a href='index.php?p= {$this->prev}' class='layui-btn layui-btn-primay  {$previd} '>上一页</a>
    		             <a href='index.php?p= {$this->next}' class='layui-btn layui-btn-primay  {$nextid} '>下一页</a>";
    	}
    }
    
    
    ?>
    

      


      首先 创建一个hello.php    ,这里已经产生了 hellos的类

    class hellos {
    public function say()
    {
    	echo "say hello";
    }
    }
    

     然后在 index.php引用这个hello.php

    require "hello.php";
     $h = new hellos();
     $h-> say();
    

    2.碰到 有很多类的情况下  我们就要 分开文件夹创建 即使相同的名字 只要设置 namesapce 对应的值就可以解决重复的问题
      比如:  

    namespace laoli;
    class sayw
    {	public function sayname()
    	{
    		echo "laoli";
    	}
    }
    

      在index    重点  下面引用 namesapce 地址对应(不是路径)

    $q=new laolisayw();  
    $q-> sayname();
    

    3.构造函数 以及函数传参
      首先 在小hello.php 写入以下
       

    namespace laolibs;
    // class sayw
    // {
    	
    // 	public function sayname()
    // 	{
    // 		echo "2019/1/20";
    // 	}
    // }
    
    class Man
    {
    	
    	function __construct($s)
    	{
    		echo "创建成功 "."$s";
    	}
    }
    

      在index.php中传参 引用构造函数  构造完 立即执行!

    $s=new laolibsMan("545s");  前面 要require 引用一下这个php文件
    

      这个意思就是 在类下面 可以写构造函数 

    4.日期函数

    // 设置时区
    date_default_timezone_set("PRC");
    
    // 设置默认时区
    date_default_timezone_get();
    // 生成时间戳
    $a=time(); 
    // 时间戳转换为日期
    echo date('Y-m-d H:i:s',1048076218); 
    

      

      


        

  • 相关阅读:
    List--使用List作为堆栈和队列
    Range的范围
    异常处理
    关于打印输出的一些方法
    关于set的unordered特性
    面向对象
    函数
    Linux中命令备份mysql形成文件
    局域网内Linux下开启ftp服务的“曲折路”和命令复习
    linux下的apache服务自启动的几种方式
  • 原文地址:https://www.cnblogs.com/nice2018/p/10296148.html
Copyright © 2011-2022 走看看