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

  

  


    

转载于:https://www.cnblogs.com/nice2018/p/10296148.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值