首页 > cms建站系统 > discuz

discuz X的单件模式

发布时间:2016-11-25 16:24

文章介绍了一个关于discuz X的单件模式原创文章,PPC高亮插件太不友好了,我直接贴txt内容都会有奇奇怪怪问题发生。

X的只要文件开头都有这么一句话

 代码如下 复制代码

$discuz = & discuz_core::instance();
[code language=php]

 

// instance()属于discuz_core类在class_core.php
function &instance() {
static $object;
if(empty($object)) {
  $object = new discuz_core();
}
return $object;
}
[/code]
这里保证单词请求都运用一个discuz_core实例。这里的&写法是为了兼容PHP4的,如果在PHP5中,则可以使用static。
[code language=php]
//这里是单件模式的简单例子。
class PHPig {
private static $v = null;
static function instance() {
  if(self::$v == null) {
   self::$v = new PHPig();
  }
  return self::$v;
}
}
$pig1 = PHPig::instance();
$pig2 = PHPig::instance();
if($pig1 === $pig2) {
echo '同一个对象';
} else {
echo '不是同一个对象';
}
[/code]

本文章详细的以实例来介绍了关于php _autoload自动加载类的用法以及如何使用_autoload来减少程序上的include和require的调用哦。

 在使用PHP的OO模式开发系统时,通常大家习惯上将每个类的实现都存放在一个单独的文件里,这样会很容易实现对类进行复用,同时将来维护时也很便利。这也是OO设计的基本思想之一。在PHP5之前,如果需要使用一个类,只需要直接使用include/require将其包含进来即可
test.class.php

 代码如下 复制代码

<?php
class abc{
function __construct()
{
echo 'www.111cn.net;
}
}
?>

load.php

 代码如下 复制代码
<?php
class LOAD
{
static function loadClass($class_name)
{
$filename = $class_name.".class.php";
if (is_file($filename)) return include_once $filename;
}
}
/**
* 设置对象的自动载入
* spl_autoload_register — Register given function as __autoload() implementation
*/
spl_autoload_register(array('LOAD', 'loadClass'));
$a = new Test();//实现自动加载,很多框架就用这种方法自动加载类
?>

__autoload()
在实际项目中,不可能把所有的类都写在一个 PHP 文件中,当在一个 PHP 文件中需要调用另一个文件中声明的类时,就需要通过 include 把这个文件引入。不过有的时候,在文件众多的项目中,要一一将所需类的文件都 include 进来,一个很大的烦恼是不得不在每个类文件开头写一个长长的包含文件的列表。我们能不能在用到什么类的时候,再把这个类所在的 php 文件导入呢?

为此,PHP 提供了 __autoload() 方法,它会在试图使用尚未被定义的类时自动调用。通过调用此函数,脚本引擎在 PHP 出错失败前有了最后一个机会加载所需的类。

__autoload() 方法接收的一个参数,就是欲加载的类的类名,所以这时候需要类名与文件名对应,如 Person.php ,对应的类名就是 Pserson 。


下面看个完整的实例

 代码如下 复制代码
class ClassA{
public function __construct(){
echo “ClassA load success!”;
}
}
//定义一个类ClassA,文件名为ClassA.php
class ClassA{
public function __construct(){
echo “ClassA load success!”;
}
}
class ClassB extends ClassA {
public function __construct(){
//parent::__construct();
echo “ClassB load success!”;
}
}
//定义一个类ClassB,文件名为ClassB.php,ClassB继承ClassA
class ClassB extends ClassA {
public function __construct(){
//parent::__construct();
echo “ClassB load success!”;
}
}

定义两个测试用的类之后,我们来编写一个含有__autoload()方法的PHP运行程序文件如下:

 代码如下 复制代码

function __autoload($classname){
$classpath=”./”.$classname.'.php';
if(file_exists($classpath)){
require_once($classpath);
}
else{
echo ‘class file'.$classpath.'not found!';
}
}

$newobj = new ClassA();
$newobj = new ClassB();

文章利用了socket来实现异步调用哦,有需要学习socket的朋友可以参考一下本文章以及参考资料哦。
 代码如下 复制代码

<?
 $host = "www.111cn.net";
 $path = "/Report.php?ReportID=1";
 $cookie = Session_id();
 
 $fp = fsockopen($host, 80, $errno, $errstr, 30);
 if (!$fp) {
    print "$errstr ($errno)<br />n";
    exit;
 }
 $out = "GET ".$path." HTTP/1.1rn";
 $out .= "Host: ".$host."rn";
 $out .= "Connection: Closern";
 $out .= "Cookie: ".$cookie."rnrn";
 
 fwrite($fp, $out);  //将请求写入socket
 //也可以选择获取server端的响应
 /*while (!feof($fp)) {
     echo fgets($fp, 128);
 }*/
 //如果不等待server端响应直接关闭socket即可
 fclose($fp);
 ?>

更详细关于php socket可以参考

http://www.111cn.net/phper/30/7cadb3c9195ac7d8ac9104da61a25c6e.htm

PHP 不会检测到用户是否已断开连接,直到尝试向客户机发送信息为止。简单地使用 echo 语句无法确保信息发送,参阅 flush() 函数。
 代码如下 复制代码

<?php
 ignore_user_abort(true);
 set_time_limit(0);
 
 while(1) {
   $fp  = fopen('time_task.txt',"a+");
   $str = date("Y-m-d h:i:s")."nr";
   fwrite($fp,$str);
   fclose($fp);
   sleep(5);    //半小时执行一次
 }
 ?>


定义和用法
ignore_user_abort() 函数设置与客户机断开是否会终止脚本的执行。

本函数返回 user-abort 设置的之前的值(一个布尔值)。

语法
ignore_user_abort(setting)参数 描述
setting 可选。如果设置为 true,则忽略与用户的断开,如果设置为 false,会导致脚本停止运行。

如果未设置该参数,会返回当前的设置。
 
提示和注释
注释:PHP 不会检测到用户是否已断开连接,直到尝试向客户机发送信息为止。简单地使用 echo 语句无法确保信息发送,参阅 flush() 函数。

 

这里有一篇文章大家可以参考一下

http://www.111cn.net/phper/php/php-ignore_user_abort.htm

本文章介绍了一个简单的关于php入门篇-缓存技术简单应用,有需要的朋友可以看看哦,这里是利用了ob_start(); ob_end_flush(); 来实例的。
 代码如下 复制代码

<?php
// define the path and name of cached file
$cachefile = 'cached-files/'.date('M-d-Y').'.php';
// define how long we want to keep the file in seconds. I set mine to 5 hours.
$cachetime = 18000;
// Check if the cached file is still fresh. If it is, serve it up and exit.
if (file_exists($cachefile) && time() - $cachetime < filemtime($cachefile)) {
include($cachefile);
exit;
}
// if there is either no file OR the file to too old, render the page and capture the HTML.
ob_start();
?>
<html>
output all your html here.
</html>
<?php
// We're done! Save the cached content to a file
$fp = fopen($cachefile, 'w');
fwrite($fp, ob_get_contents());
fclose($fp);
// finally send browser output
ob_end_flush();
?>
标签:[!--infotagslink--]

您可能感兴趣的文章: