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); 
    

      

      


        

  • 相关阅读:
    mybatis0206 延迟加载
    怎样关闭“粘滞键”?
    TNS-12557: TNS:protocol adapter not loadable TNS-12560: TNS:protocol adapter error
    HTTP协议头部与Keep-Alive模式详解
    oracle定时器执行一遍就不执行或本就不执行
    Inflation System Properties
    https://stackoverflow.com/questions/16130292/java-lang-outofmemoryerror-permgen-space-java-reflection
    java spring中对properties属性文件加密及其解密
    annotation配置springMVC的方法了事务不起作用
    SQLPlus在连接时通常有四种方式
  • 原文地址:https://www.cnblogs.com/nice2018/p/10296148.html
Copyright © 2011-2022 走看看