body {background-color: #c3c3c3}

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 laoli\sayw();  
$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 laolibs\Man("545s");  前面 要require 引用一下这个php文件

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

 

4.日期函数

// 设置时区
date_default_timezone_set("PRC");

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

  

  


    

posted @ 2019-01-20 20:56  最美胡萝卜  阅读(208)  评论(0编辑  收藏  举报
body {background-color: #c3c3c3}