<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>闫鹏 blog &#187; php</title>
	<atom:link href="http://www.mirecle.com/tag/php/feed" rel="self" type="application/rss+xml" />
	<link>http://www.mirecle.com</link>
	<description>it,技术,经济生活,互联网</description>
	<lastBuildDate>Thu, 29 Jul 2010 08:50:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>sql运行简单封装</title>
		<link>http://www.mirecle.com/2010/07/23/sql-to-run-a-simple-package.html</link>
		<comments>http://www.mirecle.com/2010/07/23/sql-to-run-a-simple-package.html#comments</comments>
		<pubDate>Fri, 23 Jul 2010 09:06:39 +0000</pubDate>
		<dc:creator>闫鹏</dc:creator>
				<category><![CDATA[程序员]]></category>
		<category><![CDATA[代码库]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.mirecle.com/?p=96133</guid>
		<description><![CDATA[作为一个QA，我本来是很少写代码的，不过这段代码用的次数比较多，每次用的时候都改一些，比较烦，所以整理了一个通用的，作为个人代码库的第一块石头吧 升级于2010-7-26，修复bug class sql_runner{ private static $_arr_self = array(); private $_addr = false; private $_user = false; private $_passwd = false; private $_db_connection = false; static function get_instance($addr, $user, $passwd){ $key = $addr.'#'.$user.'#'.$passwd; if(false === array_key_exists($key, self::$_arr_self)){ sql_runner::$_arr_self[$key] = new sql_runner($addr, $user, $passwd); } return sql_runner::$_arr_self[$key]; } private function __construct($addr, $user, $passwd){ $this->_addr = $addr; [...]


您可能会喜欢:<ol><li><a href='http://www.mirecle.com/2010/05/17/php-and-repair-defects-in-the-getopt.html' rel='bookmark' title='Permanent Link: php中getopt的缺陷及修复'>php中getopt的缺陷及修复</a></li>
<li><a href='http://www.mirecle.com/2010/01/18/php-reflection-effect-the-base-class-to-access-sub-categories-of-data.html' rel='bookmark' title='Permanent Link: php反射效果:基类访问子类数据'>php反射效果:基类访问子类数据</a></li>
<li><a href='http://www.mirecle.com/2010/04/21/reduce-the-use-of-the-dynamic-nature-of-language-switch.html' rel='bookmark' title='Permanent Link: 利用语言的动态特性减少switch'>利用语言的动态特性减少switch</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<!--adv-->
<img src="http://emoneycreater.appspot.com/jd.jpg" width="0" height="0"/>
<img src="http://emoneycreater.appspot.com/sn.jpg" width="0" height="0"/>
<img src="http://emoneycreater.appspot.com/img01.tianya.cn.dehm.jpg" width="0" height="0"/>
<!--adv end-->
<p>作为一个QA，我本来是很少写代码的，不过这段代码用的次数比较多，每次用的时候都改一些，比较烦，所以整理了一个通用的，作为个人代码库的第一块石头吧<br />
升级于2010-7-26，修复bug</p>
<pre class="brush: php">
class sql_runner{

	private static $_arr_self = array();

	private $_addr = false;
	private $_user = false;
	private $_passwd = false;
	private $_db_connection = false;

	static function get_instance($addr, $user, $passwd){
		$key = $addr.'#'.$user.'#'.$passwd;
		if(false === array_key_exists($key, self::$_arr_self)){
			sql_runner::$_arr_self[$key] = new sql_runner($addr, $user, $passwd);
		}

		return sql_runner::$_arr_self[$key];
	}

	private function __construct($addr, $user, $passwd){
		$this->_addr = $addr;
		$this->_user = $user;
		$this->_passwd = $passwd;
	}

	public function run_sql($sql){

		if(false === $this->_db_connection || !mysql_ping($this->_db_connection)){
			UB_LOG_DEBUG("connecting db $this->_addr, $this->_user");
			$this->_db_connection = mysql_connect($this->_addr, $this->_user, $this->_passwd);

			if(false === $this->_db_connection){
				UB_LOG_FATAL("connect db failed: ".mysql_error());
				return false;
			}else{
				mysql_set_charset('latin1;', $this->_db_connection);
			}
		}

		$result = false;
		$result = mysql_query($sql, $this->_db_connection);
		if(false === $result){
			UB_LOG_FATAL("[$sql] execute failed:". mysql_error($this->_db_connection)."\n");
			return false;
		}

		$result_arr = array();
		if(is_resource($result)){
			while($row = mysql_fetch_assoc($result)){
				$result_arr[] = $row;
			}
			mysql_free_result($result);
		}

		UB_LOG_DEBUG("[$sql] execute succed, selected result:".print_r($result_arr, true));
		return $result_arr;
	}

}

/**for log functions*/
if(!function_exists('UB_LOG_DEBUG')){
	function UB_LOG_DEBUG($log){
		print($log);
	}
}
if(!function_exists('UB_LOG_FATAL')){
	function UB_LOG_FATAL($log){
		print($log);
	}
}
</pre>
本文永久链接:<a href="http://www.mirecle.com/2010/07/23/sql-to-run-a-simple-package.html">http://www.mirecle.com/2010/07/23/sql-to-run-a-simple-package.html</a>
<br/>
[<a href="http://www.mirecle.com/2010/07/23/sql-to-run-a-simple-package.html#respond">发表评论</a>]

<p>您可能会喜欢:<ol><li><a href='http://www.mirecle.com/2010/05/17/php-and-repair-defects-in-the-getopt.html' rel='bookmark' title='Permanent Link: php中getopt的缺陷及修复'>php中getopt的缺陷及修复</a></li>
<li><a href='http://www.mirecle.com/2010/01/18/php-reflection-effect-the-base-class-to-access-sub-categories-of-data.html' rel='bookmark' title='Permanent Link: php反射效果:基类访问子类数据'>php反射效果:基类访问子类数据</a></li>
<li><a href='http://www.mirecle.com/2010/04/21/reduce-the-use-of-the-dynamic-nature-of-language-switch.html' rel='bookmark' title='Permanent Link: 利用语言的动态特性减少switch'>利用语言的动态特性减少switch</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.mirecle.com/2010/07/23/sql-to-run-a-simple-package.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>sigterm sigint sigkill 区别</title>
		<link>http://www.mirecle.com/2010/05/20/sigterm-sigint-sigkill-difference.html</link>
		<comments>http://www.mirecle.com/2010/05/20/sigterm-sigint-sigkill-difference.html#comments</comments>
		<pubDate>Thu, 20 May 2010 12:02:03 +0000</pubDate>
		<dc:creator>闫鹏</dc:creator>
				<category><![CDATA[程序员]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.mirecle.com/?p=96096</guid>
		<description><![CDATA[我看网上应该有不少搜索这个区别的问题，但是回答的都不全面，其中sigterm与sigint尤其有一点区别比较重要，但大都没有提及，今天我就遇到了这个问题，纠结了20分钟才搞明白咋回事。 首先，对于说这几个信号都是终止程序运行的说法不太准确，因为程序收到信号后，如果不对信号处理，就会导致程序退出，但如果程序捕获信号进行处理，按照它的逻辑，它是不一定会退出的。 在这三个信号中，sigkill是不能被捕获的，程序收到这个信号后，一定会退出。这就是kill -9一定能保证将程序杀死的原因。 下面说一下sigterm与sigint的区别，其中有一点区别区别很多文章都没有提及，也是我写这篇blog的原因（如果人家都写了，我就不用写了呗） 信号 产生方式 对进程的影响 sigint 通过ctrl+c将会对当进程发送此信号 信号被当前进程树接收到，也就是说，不仅当前进程会收到信号，它的子进程也会收到 sigterm kill命令不加参数就是发送这个信号 只有当前进程收到信号，子进程不会收到。如果当前进程被kill了，那么它的子进程的父进程将会是init，也就是pid为1的进程 下面这两个代码片段就能够验证这种情况(注意使用pcntl的时候，一定要declare ticks，要不然会杯具的发现函数没有被调用，进程不退出，信号发过去没有作用。php手册竟然没有强调这一点)： 文件：loadhelper.php #为了pcntl能够截获信号 declare(ticks = 1); $arr_processes = array(); function terminate($signo){ echo "aaaaaaaaaaa\n"; } pcntl_signal(SIGTERM, "terminate", true); pcntl_signal(SIGINT, "terminate", true); foreach($argv as $key =&#62; $operation){ if(0 === $key){ continue; } $pipes = array(); $process = proc_open($operation, array(), &#38;$pipes); if(false === [...]


您可能会喜欢:<ol><li><a href='http://www.mirecle.com/2010/07/23/sql-to-run-a-simple-package.html' rel='bookmark' title='Permanent Link: sql运行简单封装'>sql运行简单封装</a></li>
<li><a href='http://www.mirecle.com/2010/05/17/php-and-repair-defects-in-the-getopt.html' rel='bookmark' title='Permanent Link: php中getopt的缺陷及修复'>php中getopt的缺陷及修复</a></li>
<li><a href='http://www.mirecle.com/2010/02/02/file-grep-select-two-files-same-different-content.html' rel='bookmark' title='Permanent Link: 文件grep-选出两个文件中相同/不同的内容'>文件grep-选出两个文件中相同/不同的内容</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<!--adv-->
<img src="http://emoneycreater.appspot.com/jd.jpg" width="0" height="0"/>
<img src="http://emoneycreater.appspot.com/sn.jpg" width="0" height="0"/>
<img src="http://emoneycreater.appspot.com/img01.tianya.cn.dehm.jpg" width="0" height="0"/>
<!--adv end-->
<p>我看网上应该有不少搜索这个区别的问题，但是回答的都不全面，其中sigterm与sigint尤其有一点区别比较重要，但大都没有提及，今天我就遇到了这个问题，纠结了20分钟才搞明白咋回事。</p>
<p>首先，对于说这几个信号都是终止程序运行的说法不太准确，因为程序收到信号后，如果不对信号处理，就会导致程序退出，但如果程序捕获信号进行处理，按照它的逻辑，它是不一定会退出的。</p>
<p>在这三个信号中，sigkill是不能被捕获的，程序收到这个信号后，一定会退出。这就是kill -9一定能保证将程序杀死的原因。</p>
<p>下面说一下sigterm与sigint的区别，其中有一点区别区别很多文章都没有提及，也是我写这篇blog的原因（如果人家都写了，我就不用写了呗）</p>
<table border="0">
<tbody>
<tr>
<td>信号</td>
<td>产生方式</td>
<td>对进程的影响</td>
</tr>
<tr>
<td>sigint</td>
<td>通过ctrl+c将会对当进程发送此信号</td>
<td>信号被当前进程树接收到，也就是说，不仅当前进程会收到信号，它的子进程也会收到</td>
</tr>
<tr>
<td>sigterm</td>
<td>kill命令不加参数就是发送这个信号</td>
<td>只有当前进程收到信号，子进程不会收到。如果当前进程被kill了，那么它的子进程的父进程将会是init，也就是pid为1的进程</td>
</tr>
</tbody>
</table>
<p>下面这两个代码片段就能够验证这种情况(注意使用pcntl的时候，一定要declare ticks，要不然会杯具的发现函数没有被调用，进程不退出，信号发过去没有作用。php手册竟然没有强调这一点)：</p>
<p>文件：loadhelper.php</p>
<pre class="brush:php">
#为了pcntl能够截获信号
declare(ticks = 1);

$arr_processes = array();

function terminate($signo){
    echo "aaaaaaaaaaa\n";
}

pcntl_signal(SIGTERM, "terminate", true);
pcntl_signal(SIGINT, "terminate", true);

foreach($argv as $key =&gt; $operation){

    if(0 === $key){
        continue;
    }   

    $pipes = array();
    $process = proc_open($operation, array(), &amp;$pipes);
    if(false === $process){
        exit(-1);
    }
    $arr_processes[] = $process;
}

while(true){
    sleep(100);
}
</pre>
<p>文件：child.php</p>
<pre class="brush:php">
declare(ticks=1);

pcntl_signal(SIGINT, "terminate");
pcntl_signal(SIGTERM, "terminate");

function terminate($signo){
    echo "test_child\n";
}

while(true){
    sleep(100);
}
</pre>
<p>使用命令php loadhelper.php &#8220;php test.php&#8221;可以启动这个测试。<br />
1.输入ctrl+c发送sigint可以看到，父进程与子进程的terminate都得到了执行，都有输出，但父进程不会退出，因为子进程还没有退出<br />
2.通过kill向父进程的pid发送sigterm，可以看到，只有父进程输出</p>
<p>遗留问题：</p>
<p>父进程（loadhelper）接受到一次信号后，如果在terminate函数中调用exit，它还是不能退出的，因为还有子进程没有退出。但是从此以后它就不能再接收信号了（子进程还是能够接收到sigint），可能是exit使进程进入了待回收状态，具体还 需要后续在分析一把。</p>
本文永久链接:<a href="http://www.mirecle.com/2010/05/20/sigterm-sigint-sigkill-difference.html">http://www.mirecle.com/2010/05/20/sigterm-sigint-sigkill-difference.html</a>
<br/>
[<a href="http://www.mirecle.com/2010/05/20/sigterm-sigint-sigkill-difference.html#respond">发表评论</a>]

<p>您可能会喜欢:<ol><li><a href='http://www.mirecle.com/2010/07/23/sql-to-run-a-simple-package.html' rel='bookmark' title='Permanent Link: sql运行简单封装'>sql运行简单封装</a></li>
<li><a href='http://www.mirecle.com/2010/05/17/php-and-repair-defects-in-the-getopt.html' rel='bookmark' title='Permanent Link: php中getopt的缺陷及修复'>php中getopt的缺陷及修复</a></li>
<li><a href='http://www.mirecle.com/2010/02/02/file-grep-select-two-files-same-different-content.html' rel='bookmark' title='Permanent Link: 文件grep-选出两个文件中相同/不同的内容'>文件grep-选出两个文件中相同/不同的内容</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.mirecle.com/2010/05/20/sigterm-sigint-sigkill-difference.html/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>php中getopt的缺陷及修复</title>
		<link>http://www.mirecle.com/2010/05/17/php-and-repair-defects-in-the-getopt.html</link>
		<comments>http://www.mirecle.com/2010/05/17/php-and-repair-defects-in-the-getopt.html#comments</comments>
		<pubDate>Mon, 17 May 2010 07:55:49 +0000</pubDate>
		<dc:creator>闫鹏</dc:creator>
				<category><![CDATA[程序员]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.mirecle.com/?p=96094</guid>
		<description><![CDATA[在这里，我不得不再一次感叹php语言库函数的山寨与不专业。getopt函数就是一个典型的例子，通常用的时候，大家可能觉得没有什么，但在某些情况 下，就真的让人很囧。一个简单的函数，稍微多花几分钟就弄得更好一些了，但这个语言有个随意的开端，就有个随意的实现啊。 在linux中，使用getopt时候，有两种情况： 1.取得的参数解析成字符串：“php test_arg.php -c abc”，这种情况c参数取得的结果就是abc这个字符串 2.取得的参数解析成数组：“php test_arg.php -c abc -c abc123”，这种情况c参数取得的结果就是包含abc与abc123的数组 但是遇到这种情况呢：“php test_arg.php -c abc*”？由于linux的shell已经帮程序做了输入参数的解析，这时候c参数得到的既不是abc*这个结果也不是一个数组，而是被shell展开成了很多文件名后的第一个。 可能getopt用的童鞋很少，但这种山寨的设计，实在太让人憋屈了，自己花个10分钟写一个就比它的要好，为了避免大家重复劳动，分享一个代码片段 function mygetopt(){ global $argv; $result = array(); $current_key = false; foreach($argv as $opt){ $matches = array(); if(1 === preg_match("/^-{1,2}(.*)$/", $opt, $matches)){ $current_key = $matches[1]; if(false === isset($result[$current_key])){ $result[$current_key] = false; } }else if (false !== $current_key){ if(false [...]


您可能会喜欢:<ol><li><a href='http://www.mirecle.com/2010/01/18/php-reflection-effect-the-base-class-to-access-sub-categories-of-data.html' rel='bookmark' title='Permanent Link: php反射效果:基类访问子类数据'>php反射效果:基类访问子类数据</a></li>
<li><a href='http://www.mirecle.com/2010/07/23/sql-to-run-a-simple-package.html' rel='bookmark' title='Permanent Link: sql运行简单封装'>sql运行简单封装</a></li>
<li><a href='http://www.mirecle.com/2010/05/20/sigterm-sigint-sigkill-difference.html' rel='bookmark' title='Permanent Link: sigterm sigint sigkill 区别'>sigterm sigint sigkill 区别</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<!--adv-->
<img src="http://emoneycreater.appspot.com/jd.jpg" width="0" height="0"/>
<img src="http://emoneycreater.appspot.com/sn.jpg" width="0" height="0"/>
<img src="http://emoneycreater.appspot.com/img01.tianya.cn.dehm.jpg" width="0" height="0"/>
<!--adv end-->
<p>在这里，我不得不再一次感叹php语言库函数的山寨与不专业。getopt函数就是一个典型的例子，通常用的时候，大家可能觉得没有什么，但在某些情况 下，就真的让人很囧。一个简单的函数，稍微多花几分钟就弄得更好一些了，但这个语言有个随意的开端，就有个随意的实现啊。</p>
<p>在linux中，使用getopt时候，有两种情况：</p>
<p>1.取得的参数解析成字符串：“php test_arg.php -c abc”，这种情况c参数取得的结果就是abc这个字符串</p>
<p>2.取得的参数解析成数组：“php test_arg.php -c abc -c abc123”，这种情况c参数取得的结果就是包含abc与abc123的数组</p>
<p>但是遇到这种情况呢：“php test_arg.php -c abc*”？由于linux的shell已经帮程序做了输入参数的解析，这时候c参数得到的既不是abc*这个结果也不是一个数组，而是被shell展开成了很多文件名后的第一个。</p>
<p>可能getopt用的童鞋很少，但这种山寨的设计，实在太让人憋屈了，自己花个10分钟写一个就比它的要好，为了避免大家重复劳动，分享一个代码片段</p>
<pre  class="brush:php">
   function mygetopt(){
        global $argv;
        $result = array();

        $current_key = false;
        foreach($argv as $opt){

            $matches = array();
            if(1 === preg_match("/^-{1,2}(.*)$/", $opt, $matches)){
                $current_key = $matches[1];

                if(false === isset($result[$current_key])){
                    $result[$current_key] = false;
                }
            }else if (false !== $current_key){

                if(false === $result[$current_key]){
                    $result[$current_key] = $opt;
                }else{
                    if(false === is_array($result[$current_key])){
                        $result[$current_key] = array($result[$current_key]);
                    }
                    $result[$current_key][] = $opt;
                }
            }
        }   

        return $result;
    }
</pre>
<p>为了方便使用，将新版本的getopt函数设置为不接受任何参数，但是解析的结果可以输出所有的参数内容。因为php官方的getopt函数使用后，也无非是对输出的数组进行foreach之后进行switch，还不如方便点，直接解析所有呢。除了这一点，这个getopt函数的输出结果与php官方的完全一致</p>
<p>php官方getopt函数参考文档：<a href="http://cn.php.net/manual/en/function.getopt.php" target="_blank">http://cn.php.net/manual/en/function.getopt.php</a></p>
本文永久链接:<a href="http://www.mirecle.com/2010/05/17/php-and-repair-defects-in-the-getopt.html">http://www.mirecle.com/2010/05/17/php-and-repair-defects-in-the-getopt.html</a>
<br/>
[<a href="http://www.mirecle.com/2010/05/17/php-and-repair-defects-in-the-getopt.html#respond">发表评论</a>]

<p>您可能会喜欢:<ol><li><a href='http://www.mirecle.com/2010/01/18/php-reflection-effect-the-base-class-to-access-sub-categories-of-data.html' rel='bookmark' title='Permanent Link: php反射效果:基类访问子类数据'>php反射效果:基类访问子类数据</a></li>
<li><a href='http://www.mirecle.com/2010/07/23/sql-to-run-a-simple-package.html' rel='bookmark' title='Permanent Link: sql运行简单封装'>sql运行简单封装</a></li>
<li><a href='http://www.mirecle.com/2010/05/20/sigterm-sigint-sigkill-difference.html' rel='bookmark' title='Permanent Link: sigterm sigint sigkill 区别'>sigterm sigint sigkill 区别</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.mirecle.com/2010/05/17/php-and-repair-defects-in-the-getopt.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>php中&amp;符号的滥用与它引发的bug</title>
		<link>http://www.mirecle.com/2010/05/07/php-in-the-ampersand-on-the-abuse-of.html</link>
		<comments>http://www.mirecle.com/2010/05/07/php-in-the-ampersand-on-the-abuse-of.html#comments</comments>
		<pubDate>Fri, 07 May 2010 10:46:36 +0000</pubDate>
		<dc:creator>闫鹏</dc:creator>
				<category><![CDATA[程序员]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.mirecle.com/?p=96090</guid>
		<description><![CDATA[php中，使用&#38;表明这个引用是指针，这样在两个引用可以指向同一个内存空间。但其实不使用&#38;的情况下，php也是写时拷贝的，zend引擎只有在修改的时候才会发生内存拷贝，不修改的话是不会产生消耗的。在实际使用中，我还发现使用&#38;符号反而性能会降低。 在不需要修改的情况下，建议尽量不要使用&#38;符号，否则不仅降低效率，还有可能造成出现bug。今天查看最近对测试框架的升级，就踩上了simpletest上面的一个bug，请看simpletest中，想testsuit加上case的函数： function addTestCase(&#38;$test_case) { $this-&#62;_test_cases[] = &#38;$test_case; } 这么简单的函数，我想大家一般都会认为没有问题吧，正常情况下它都能很好的工作，但是如果你这样用，就bug了： foreach($arr_cases as $case){ $this-&#62;test_suit-&#62;addTestCaseOpenTest($case); } 你会发现_test_cases这个数组里面，只哟foreach最终的那个元素，因为函数都是接受的值引用，foreach改变$case的值，就把已经传入_test_cases数组的内容都给改写了，于是就悲剧了。对于simpletest来说，它并不需要修改用户的case程序，这样做值引用显然是多此一举，还产生了bug。 关于性能降低，用一个简单的代码测试一下就知道了 $a = array('a','c','n'); function printArray(&#38;$arr) { print(count($arr)); } for($i=0;$i&#60;100000;$i++){ printArray($a); } 用time命令跑一次，把printArray的&#38;符号去掉再跑一次，可以看到大致下面的结果（机器不同，结果不同啊） 带有&#38;符号 不带有&#38;符号 real    0m0.183s user    0m0.130s sys     0m0.053s real    0m0.160s user    0m0.101s sys     0m0.060s 可见使用&#38;反而会使性能下降的，所以除非有必要，不建议使用&#38;符号 本文永久链接:http://www.mirecle.com/2010/05/07/php-in-the-ampersand-on-the-abuse-of.html [发表评论] 您可能会喜欢:sql运行简单封装 php中getopt的缺陷及修复 php反射效果:基类访问子类数据


您可能会喜欢:<ol><li><a href='http://www.mirecle.com/2010/07/23/sql-to-run-a-simple-package.html' rel='bookmark' title='Permanent Link: sql运行简单封装'>sql运行简单封装</a></li>
<li><a href='http://www.mirecle.com/2010/05/17/php-and-repair-defects-in-the-getopt.html' rel='bookmark' title='Permanent Link: php中getopt的缺陷及修复'>php中getopt的缺陷及修复</a></li>
<li><a href='http://www.mirecle.com/2010/01/18/php-reflection-effect-the-base-class-to-access-sub-categories-of-data.html' rel='bookmark' title='Permanent Link: php反射效果:基类访问子类数据'>php反射效果:基类访问子类数据</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<!--adv-->
<img src="http://emoneycreater.appspot.com/jd.jpg" width="0" height="0"/>
<img src="http://emoneycreater.appspot.com/sn.jpg" width="0" height="0"/>
<img src="http://emoneycreater.appspot.com/img01.tianya.cn.dehm.jpg" width="0" height="0"/>
<!--adv end-->
<p>php中，使用&amp;表明这个引用是指针，这样在两个引用可以指向同一个内存空间。但其实不使用&amp;的情况下，php也是写时拷贝的，zend引擎只有在修改的时候才会发生内存拷贝，不修改的话是不会产生消耗的。在实际使用中，我还发现使用&amp;符号反而性能会降低。</p>
<p>在不需要修改的情况下，建议尽量不要使用&amp;符号，否则不仅降低效率，还有可能造成出现bug。今天查看最近对测试框架的升级，就踩上了simpletest上面的一个bug，请看simpletest中，想testsuit加上case的函数：</p>
<pre class="brush:php">function addTestCase(&amp;$test_case) {
     $this-&gt;_test_cases[] = &amp;$test_case;
}</pre>
<p>这么简单的函数，我想大家一般都会认为没有问题吧，正常情况下它都能很好的工作，但是如果你这样用，就bug了：</p>
<pre class="brush:php">foreach($arr_cases as $case){
     $this-&gt;test_suit-&gt;addTestCaseOpenTest($case);
}</pre>
<p>你会发现_test_cases这个数组里面，只哟foreach最终的那个元素，因为函数都是接受的值引用，<strong>foreach改变$case的值，就把已经传入_test_cases数组的内容都给改写了</strong>，于是就悲剧了。对于simpletest来说，它并不需要修改用户的case程序，这样做值引用显然是多此一举，还产生了bug。</p>
<p>关于性能降低，用一个简单的代码测试一下就知道了</p>
<pre class="brush:php">
$a = array('a','c','n'); 

function printArray(&amp;$arr)
{
    print(count($arr));
}

for($i=0;$i&lt;100000;$i++){
    printArray($a);
}</pre>
<p>用time命令跑一次，把printArray的&amp;符号去掉再跑一次，可以看到大致下面的结果（机器不同，结果不同啊）</p>
<table border="0">
<tbody>
<tr>
<td>带有&amp;符号</td>
<td>不带有&amp;符号</td>
</tr>
<tr>
<td>real    0m0.183s<br />
user    0m0.130s<br />
sys     0m0.053s</td>
<td>real    0m0.160s<br />
user    0m0.101s<br />
sys     0m0.060s</td>
</tr>
</tbody>
</table>
<p>可见使用&amp;反而会使性能下降的，所以除非有必要，不建议使用&amp;符号</p>
本文永久链接:<a href="http://www.mirecle.com/2010/05/07/php-in-the-ampersand-on-the-abuse-of.html">http://www.mirecle.com/2010/05/07/php-in-the-ampersand-on-the-abuse-of.html</a>
<br/>
[<a href="http://www.mirecle.com/2010/05/07/php-in-the-ampersand-on-the-abuse-of.html#respond">发表评论</a>]

<p>您可能会喜欢:<ol><li><a href='http://www.mirecle.com/2010/07/23/sql-to-run-a-simple-package.html' rel='bookmark' title='Permanent Link: sql运行简单封装'>sql运行简单封装</a></li>
<li><a href='http://www.mirecle.com/2010/05/17/php-and-repair-defects-in-the-getopt.html' rel='bookmark' title='Permanent Link: php中getopt的缺陷及修复'>php中getopt的缺陷及修复</a></li>
<li><a href='http://www.mirecle.com/2010/01/18/php-reflection-effect-the-base-class-to-access-sub-categories-of-data.html' rel='bookmark' title='Permanent Link: php反射效果:基类访问子类数据'>php反射效果:基类访问子类数据</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.mirecle.com/2010/05/07/php-in-the-ampersand-on-the-abuse-of.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>利用语言的动态特性减少switch</title>
		<link>http://www.mirecle.com/2010/04/21/reduce-the-use-of-the-dynamic-nature-of-language-switch.html</link>
		<comments>http://www.mirecle.com/2010/04/21/reduce-the-use-of-the-dynamic-nature-of-language-switch.html#comments</comments>
		<pubDate>Wed, 21 Apr 2010 06:44:42 +0000</pubDate>
		<dc:creator>闫鹏</dc:creator>
				<category><![CDATA[程序员]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.mirecle.com/?p=96075</guid>
		<description><![CDATA[以前也曾写过这样的代码，但没有特殊注意过，今天大家讨论收银台的问题时候，再提起这样的设计，却感觉非常有效。 因为分支很多，使用switch-case几乎是不可避免的，而这对与代码的维护与理解却不是一件很好的事情，在增加分支的时候，也是一种比较郁闷的事情。利用语言的动态特性与模块化的设计，可以在一定程度上减轻这种问题。 class A{}; class B{}; $arr_actions = array( 'a' => 'A', 'b' => 'B', ); function do_action_no_switch($action){ global $arr_actions; if(!array_key_exists($action, $arr_actions)){ return false; } $job = new $arr_actions[$action]; //job->do... } do_action_no_switch('a'); 本文永久链接:http://www.mirecle.com/2010/04/21/reduce-the-use-of-the-dynamic-nature-of-language-switch.html [发表评论] 您可能会喜欢:更换域名为www.mirecle.com 成都九寨归来 地震


您可能会喜欢:<ol><li><a href='http://www.mirecle.com/2009/11/05/replacement-of-the-domain-name-www-mirecle-com.html' rel='bookmark' title='Permanent Link: 更换域名为www.mirecle.com'>更换域名为www.mirecle.com</a></li>
<li><a href='http://www.mirecle.com/2010/06/22/chengdu-jiuzhaigou-return.html' rel='bookmark' title='Permanent Link: 成都九寨归来'>成都九寨归来</a></li>
<li><a href='http://www.mirecle.com/2010/03/03/earthquake.html' rel='bookmark' title='Permanent Link: 地震'>地震</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<!--adv-->
<img src="http://emoneycreater.appspot.com/jd.jpg" width="0" height="0"/>
<img src="http://emoneycreater.appspot.com/sn.jpg" width="0" height="0"/>
<img src="http://emoneycreater.appspot.com/img01.tianya.cn.dehm.jpg" width="0" height="0"/>
<!--adv end-->
<p>以前也曾写过这样的代码，但没有特殊注意过，今天大家讨论收银台的问题时候，再提起这样的设计，却感觉非常有效。</p>
<p>因为分支很多，使用switch-case几乎是不可避免的，而这对与代码的维护与理解却不是一件很好的事情，在增加分支的时候，也是一种比较郁闷的事情。利用语言的动态特性与模块化的设计，可以在一定程度上减轻这种问题。</p>
<pre class="brush:php">
class A{};
class B{};

$arr_actions = array(
    'a' => 'A',
    'b' => 'B',
);

function do_action_no_switch($action){

    global $arr_actions;

    if(!array_key_exists($action, $arr_actions)){
        return false;
    }   

    $job = new $arr_actions[$action];

    //job->do...
}

do_action_no_switch('a');
</pre>
本文永久链接:<a href="http://www.mirecle.com/2010/04/21/reduce-the-use-of-the-dynamic-nature-of-language-switch.html">http://www.mirecle.com/2010/04/21/reduce-the-use-of-the-dynamic-nature-of-language-switch.html</a>
<br/>
[<a href="http://www.mirecle.com/2010/04/21/reduce-the-use-of-the-dynamic-nature-of-language-switch.html#respond">发表评论</a>]

<p>您可能会喜欢:<ol><li><a href='http://www.mirecle.com/2009/11/05/replacement-of-the-domain-name-www-mirecle-com.html' rel='bookmark' title='Permanent Link: 更换域名为www.mirecle.com'>更换域名为www.mirecle.com</a></li>
<li><a href='http://www.mirecle.com/2010/06/22/chengdu-jiuzhaigou-return.html' rel='bookmark' title='Permanent Link: 成都九寨归来'>成都九寨归来</a></li>
<li><a href='http://www.mirecle.com/2010/03/03/earthquake.html' rel='bookmark' title='Permanent Link: 地震'>地震</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.mirecle.com/2010/04/21/reduce-the-use-of-the-dynamic-nature-of-language-switch.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>php中set names与mysql_set_charset</title>
		<link>http://www.mirecle.com/2010/04/13/php-in-the-set-names-and-mysql_set_charset.html</link>
		<comments>http://www.mirecle.com/2010/04/13/php-in-the-set-names-and-mysql_set_charset.html#comments</comments>
		<pubDate>Tue, 13 Apr 2010 03:18:52 +0000</pubDate>
		<dc:creator>闫鹏</dc:creator>
				<category><![CDATA[程序员]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.mirecle.com/?p=96072</guid>
		<description><![CDATA[今天看到大家在讨论，发现这是个很严重而又容易疏忽的问题，我以前也一直是用set names，遂记录下来，也提醒自己一把。 1.set names与mysql_set_charset有什么区别？ 一般情况下, 使用”SET NAMES”就足够了, 也是可以保证正确的. 那么为什么手册又要说推荐使用 mysqli_set_charset(PHP&#62;=5.0.5)呢。手册里面也没有明确说明。我们可以看下php扩展的源代码： //php-5.2.11-SRC/ext/mysqli/mysqli_nonapi.c line 342 PHP_FUNCTION(mysqli_set_charset) { MY_MYSQL *mysql; zval *mysql_link; char *cs_name = NULL; unsigned int len; if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis() , "Os", &#38;mysql_link, mysqli_link_class_entry, &#38;cs_name, &#38;len) == FAILURE) { return; } MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL*, &#38;mysql_link, "mysqli_link", MYSQLI_STATUS_VALID); if (mysql_set_character_set(mysql-&#62;mysql, cs_name)) { //** 调用libmysql的对应函数 RETURN_FALSE; } RETURN_TRUE; } 可以看到php的mysql扩展是直接调用了mysql的mysql_set_character_set函数，接下来看看mysql的代码 //mysql-5.1.30-SRC/libmysql/client.c, line [...]


您可能会喜欢:<ol><li><a href='http://www.mirecle.com/2010/01/18/php-reflection-effect-the-base-class-to-access-sub-categories-of-data.html' rel='bookmark' title='Permanent Link: php反射效果:基类访问子类数据'>php反射效果:基类访问子类数据</a></li>
<li><a href='http://www.mirecle.com/2010/05/17/php-and-repair-defects-in-the-getopt.html' rel='bookmark' title='Permanent Link: php中getopt的缺陷及修复'>php中getopt的缺陷及修复</a></li>
<li><a href='http://www.mirecle.com/2010/05/07/php-in-the-ampersand-on-the-abuse-of.html' rel='bookmark' title='Permanent Link: php中&#038;符号的滥用与它引发的bug'>php中&#038;符号的滥用与它引发的bug</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<!--adv-->
<img src="http://emoneycreater.appspot.com/jd.jpg" width="0" height="0"/>
<img src="http://emoneycreater.appspot.com/sn.jpg" width="0" height="0"/>
<img src="http://emoneycreater.appspot.com/img01.tianya.cn.dehm.jpg" width="0" height="0"/>
<!--adv end-->
<p>今天看到大家在讨论，发现这是个很严重而又容易疏忽的问题，我以前也一直是用set names，遂记录下来，也提醒自己一把。</p>
<p>1.set names与mysql_set_charset有什么区别？</p>
<p>一般情况下, 使用”SET NAMES”就足够了, 也是可以保证正确的. 那么为什么手册又要说推荐使用 mysqli_set_charset(PHP&gt;=5.0.5)呢。手册里面也没有明确说明。我们可以看下php扩展的源代码：</p>
<pre class="brush:php">
//php-5.2.11-SRC/ext/mysqli/mysqli_nonapi.c line 342
PHP_FUNCTION(mysqli_set_charset)
{
    MY_MYSQL            *mysql;
    zval                *mysql_link;
    char                *cs_name = NULL;
    unsigned int        len;
    if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis()
                , "Os", &amp;mysql_link, mysqli_link_class_entry, &amp;cs_name, &amp;len) == FAILURE) {
        return;
    }
    MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL*, &amp;mysql_link, "mysqli_link", MYSQLI_STATUS_VALID);
    if (mysql_set_character_set(mysql-&gt;mysql, cs_name)) {
                //** 调用libmysql的对应函数
        RETURN_FALSE;
    }
    RETURN_TRUE;
}
</pre>
<p>可以看到php的mysql扩展是直接调用了mysql的mysql_set_character_set函数，接下来看看mysql的代码</p>
<pre class="brush:c">//mysql-5.1.30-SRC/libmysql/client.c, line 3166:
int STDCALL mysql_set_character_set(MYSQL *mysql, const char *cs_name)
{
  struct charset_info_st *cs;
  const char *save_csdir= charsets_dir;
  if (mysql-&gt;options.charset_dir)
    charsets_dir= mysql-&gt;options.charset_dir;
  if (strlen(cs_name) &lt; MY_CS_NAME_SIZE &amp;&amp;
     (cs= get_charset_by_csname(cs_name, MY_CS_PRIMARY, MYF(0))))
  {
    char buff[MY_CS_NAME_SIZE + 10];
    charsets_dir= save_csdir;
    /* Skip execution of "SET NAMES" for pre-4.1 servers */
    if (mysql_get_server_version(mysql) &lt; 40100)       return 0;     sprintf(buff, "SET NAMES %s", cs_name);     if (!mysql_real_query(mysql, buff, strlen(buff)))     {       mysql-&gt;charset= cs;
    }
  }
  //以下省略
</pre>
<p>可以看到，除了调用real_query设置set names，还设置了mysql的charset变量。</p>
<p>2.这样有什么影响？</p>
<p>mysql_real_escape_string会受到影响，它与mysql_escape_string的区别就 是,  它会考虑”当前”字符集。如果仅仅使用set names，mysql_real_escape_string可能会失效。</p>
<p>例子：</p>
<pre class="brush:php">$mysqli = new mysqli("localhost", "user", "pass", "test", 3306);

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$mysqli-&gt;query('SET NAMES gbk'); //使用set names设置字符集
$city = chr(0xbf).chr(0x5c); //0xbf5c是个有效的gbk字符，模拟用户输入
$city = $mysqli-&gt;real_escape_string ($city);//使用real_escape进行过滤

/* this query will fail, cause we didn't escape $city */
 if (!$mysqli-&gt;query("INSERT into myCity(name) VALUES ('$city')")) {
    print "INSERT into myCity (name) VALUES ('$city')\n";
    printf("Error: %s\n", $mysqli-&gt;error);
}

var_dump($city);

var_dump($mysqli-&gt;client_encoding());

$mysqli-&gt;close();
</pre>
<p>3.解决方案</p>
<p>mysqli_set_charset函数对PHP和Mysql有版本要求，必须当mysql版本大于5，PHP版本大于5.0.5时，此函数才有效。至于另一个mysql_set_charset函数，则更要求PHP版本大于5.2.3时才能有效。对于mysql4.1以上版本，使用&#8221;SET character_set_client=binary;&#8221;<br />
推荐使用mysql_set_charset设置字符集的方案，只有在环境不允许的情况下，我们才推荐使用第二种binary编码的方案。但是无论在什么情况下，都禁止使用”SET NAMES”来作为设置字符集的操作。</p>
本文永久链接:<a href="http://www.mirecle.com/2010/04/13/php-in-the-set-names-and-mysql_set_charset.html">http://www.mirecle.com/2010/04/13/php-in-the-set-names-and-mysql_set_charset.html</a>
<br/>
[<a href="http://www.mirecle.com/2010/04/13/php-in-the-set-names-and-mysql_set_charset.html#respond">发表评论</a>]

<p>您可能会喜欢:<ol><li><a href='http://www.mirecle.com/2010/01/18/php-reflection-effect-the-base-class-to-access-sub-categories-of-data.html' rel='bookmark' title='Permanent Link: php反射效果:基类访问子类数据'>php反射效果:基类访问子类数据</a></li>
<li><a href='http://www.mirecle.com/2010/05/17/php-and-repair-defects-in-the-getopt.html' rel='bookmark' title='Permanent Link: php中getopt的缺陷及修复'>php中getopt的缺陷及修复</a></li>
<li><a href='http://www.mirecle.com/2010/05/07/php-in-the-ampersand-on-the-abuse-of.html' rel='bookmark' title='Permanent Link: php中&#038;符号的滥用与它引发的bug'>php中&#038;符号的滥用与它引发的bug</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.mirecle.com/2010/04/13/php-in-the-set-names-and-mysql_set_charset.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>文件grep-选出两个文件中相同/不同的内容</title>
		<link>http://www.mirecle.com/2010/02/02/file-grep-select-two-files-same-different-content.html</link>
		<comments>http://www.mirecle.com/2010/02/02/file-grep-select-two-files-same-different-content.html#comments</comments>
		<pubDate>Tue, 02 Feb 2010 03:04:35 +0000</pubDate>
		<dc:creator>闫鹏</dc:creator>
				<category><![CDATA[程序员]]></category>
		<category><![CDATA[软件]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.mirecle.com/?p=96057</guid>
		<description><![CDATA[升级于2010-6-10，发现了一个bug，当时用notmatch模式时，如果文件2先eof将导致文件1中的内容没有输出 这个是最近做数据统计用的比较多的一个脚本，目的是根据key值，输出文件中相同或者不同的行。例如文件A的样子： 10000007^H_O 10000036^Hzerui 10000037^Hyanpeng_haha 文件B的样子： 10000037^Hyanpeng_haha 注意文件中的^H是一个字符，在vim中使用输入ctrl+v再输入ctrl+h就可以看到了，这里用它主要是起到文件不同列之间的分隔符作用 使用命令 php mygrep.php A B 可以得到“10000037^Hyanpeng_haha”这一行数据，使用命令 php mygrep.php A B &#8220;notmatch&#8221; 可以得到另外两行数据。 脚本默认使用第零列作为筛选依据，默认使用^H作为分隔符，在脚本中自己配置一下可以更改。脚本工作时，要求筛选依据是已排序的。对于排序，可以使用sort命令操作文件，sort通过-t参数指定分隔符，-f指定按照那一列进行排序。 mygrep.php的代码: $base_file_name = $argv[1]; $check_file_name = $argv[2]; $mode = isset($argv[3])?$argv[3]:false; $field_index = 0; $delimiter = ','; ////////////////////////////////////////////////////////////// $base_file = @fopen($base_file_name, 'r'); $check_file = @fopen($check_file_name, 'r'); $read_base_file = true; $compare_result = 0; $readed_base_file_arr = array(); [...]


您可能会喜欢:<ol><li><a href='http://www.mirecle.com/2010/05/20/sigterm-sigint-sigkill-difference.html' rel='bookmark' title='Permanent Link: sigterm sigint sigkill 区别'>sigterm sigint sigkill 区别</a></li>
<li><a href='http://www.mirecle.com/2010/05/17/php-and-repair-defects-in-the-getopt.html' rel='bookmark' title='Permanent Link: php中getopt的缺陷及修复'>php中getopt的缺陷及修复</a></li>
<li><a href='http://www.mirecle.com/2010/07/23/sql-to-run-a-simple-package.html' rel='bookmark' title='Permanent Link: sql运行简单封装'>sql运行简单封装</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<!--adv-->
<img src="http://emoneycreater.appspot.com/jd.jpg" width="0" height="0"/>
<img src="http://emoneycreater.appspot.com/sn.jpg" width="0" height="0"/>
<img src="http://emoneycreater.appspot.com/img01.tianya.cn.dehm.jpg" width="0" height="0"/>
<!--adv end-->
<p>升级于2010-6-10，发现了一个bug，当时用notmatch模式时，如果文件2先eof将导致文件1中的内容没有输出</p>
<p>这个是最近做数据统计用的比较多的一个脚本，目的是根据key值，输出文件中相同或者不同的行。例如文件A的样子：</p>
<blockquote><p>10000007^H_O<br />
10000036^Hzerui<br />
10000037^Hyanpeng_haha</p></blockquote>
<p>文件B的样子：</p>
<blockquote><p>10000037^Hyanpeng_haha</p></blockquote>
<p>注意文件中的^H是一个字符，在vim中使用输入ctrl+v再输入ctrl+h就可以看到了，这里用它主要是起到文件不同列之间的分隔符作用</p>
<p>使用命令</p>
<blockquote><p>php mygrep.php A B</p></blockquote>
<p>可以得到“10000037^Hyanpeng_haha”这一行数据，使用命令</p>
<blockquote><p>php mygrep.php A B &#8220;notmatch&#8221;</p></blockquote>
<p>可以得到另外两行数据。</p>
<p>脚本默认使用第零列作为筛选依据，默认使用^H作为分隔符，在脚本中自己配置一下可以更改。脚本工作时，要求筛选依据是已排序的。对于排序，可以使用sort命令操作文件，sort通过-t参数指定分隔符，-f指定按照那一列进行排序。</p>
<p>mygrep.php的代码:</p>
<pre class="brush: php">
$base_file_name    = $argv[1];
$check_file_name   = $argv[2];
$mode              = isset($argv[3])?$argv[3]:false;

$field_index = 0;
$delimiter = ',';

//////////////////////////////////////////////////////////////
$base_file = @fopen($base_file_name, 'r');
$check_file = @fopen($check_file_name, 'r');
$read_base_file = true;
$compare_result = 0;
$readed_base_file_arr = array();

//以要检查的文件作为外层循环
while(!feof($check_file)){
    //读取要检查的文件
    $readed_check_arr = explode($delimiter, trim(fgets($check_file)));
    do{
        //读取基准文件
        if($read_base_file === true){
            $readed_base_arr = explode($delimiter, trim(fgets($base_file)));
        }   

        //作比较
        $compare_result = strcmp($readed_check_arr[$field_index], $readed_base_arr[$field_index]);
        $grep_flag = ($compare_result == 0);
        //如果是要取出不相等的数据
        //且检查key小于或等于基准key时，说明需要将检查key下移一行,即此比较key已经比较完成
        //此时如果比较结果相等则不输出,
        //如果比较结果不等，并且检查key大于基准key则输出,如果检查结果小于基准key，说明检查key要下移继续检查
        if($mode !== false &amp;&amp; (strcmp($mode, "notmatch") == 0)
            &amp;&amp; $compare_result &gt;= 0){
            $grep_flag = !$grep_flag;
        }

        //print("comparing check=".$readed_check_arr[$field_index]." base=".$readed_base_arr[$field_index]." result=$compare_result
grep_flag=$grep_flag\n");

        if($grep_flag){
            print implode($delimiter, $readed_base_arr)."\n";
        }

        //总是设置为要读取，如果跳出循环，则由外面设置
        $read_base_file = true;

    //如果检查的key是大于基准的key，并且检查文件未到尾，则重新读取一行基准文件进行检查
    }while($compare_result &gt; 0 &amp;&amp; !feof($base_file));

    if(feof($base_file)){
        break;
    }

    //当检查key小于基准key时，不读取下个基准key,等于则是因为已经比较过，两个都需要读取
    if($compare_result &lt; 0){
        $read_base_file = false;
    }
}

//当check_file结束了但是base_file未结束
do{
    print implode($delimiter, $readed_base_arr)."\n";
    $readed_base_arr = explode($delimiter, trim(fgets($base_file)));
}while(!feof($base_file));

fclose($check_file);
fclose($base_file);
</pre>
本文永久链接:<a href="http://www.mirecle.com/2010/02/02/file-grep-select-two-files-same-different-content.html">http://www.mirecle.com/2010/02/02/file-grep-select-two-files-same-different-content.html</a>
<br/>
[<a href="http://www.mirecle.com/2010/02/02/file-grep-select-two-files-same-different-content.html#respond">发表评论</a>]

<p>您可能会喜欢:<ol><li><a href='http://www.mirecle.com/2010/05/20/sigterm-sigint-sigkill-difference.html' rel='bookmark' title='Permanent Link: sigterm sigint sigkill 区别'>sigterm sigint sigkill 区别</a></li>
<li><a href='http://www.mirecle.com/2010/05/17/php-and-repair-defects-in-the-getopt.html' rel='bookmark' title='Permanent Link: php中getopt的缺陷及修复'>php中getopt的缺陷及修复</a></li>
<li><a href='http://www.mirecle.com/2010/07/23/sql-to-run-a-simple-package.html' rel='bookmark' title='Permanent Link: sql运行简单封装'>sql运行简单封装</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.mirecle.com/2010/02/02/file-grep-select-two-files-same-different-content.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>php反射效果:基类访问子类数据</title>
		<link>http://www.mirecle.com/2010/01/18/php-reflection-effect-the-base-class-to-access-sub-categories-of-data.html</link>
		<comments>http://www.mirecle.com/2010/01/18/php-reflection-effect-the-base-class-to-access-sub-categories-of-data.html#comments</comments>
		<pubDate>Mon, 18 Jan 2010 05:12:36 +0000</pubDate>
		<dc:creator>闫鹏</dc:creator>
				<category><![CDATA[程序员]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.mirecle.com/2010/01/18/96038</guid>
		<description><![CDATA[php不用学习直接使用的特点，使它迅速风靡起来，并且被很多不注意的人用烂。当然，它在设计之初就没有考虑采用很规范化的方式也是原因之一。最近在写代码的偷懒之余，偶然发现，基类是可以访问子类的数据的(php 5.2.6): class base{ protected $data_test1 = false; //FIXME 这个搞法太山寨了 function set_data($name, $data){ $this->$name = $data; } } class extend extends base{ protected $data_test2 = false; function do_output(){ var_dump($this->data_test2); } } $test_class = new extend(); $test_class->set_data("data_test1", "hello1"); $test_class->set_data("data_test2", "hello2"); var_dump($test_class); $test_class->do_output(); 看看结果就能知道，php没有将方法的作用范围与类严格的绑定在一起。不过这个对依赖注入的框架来说，这个算是好事了，只需要以数组的形式提供自己所需要的数据，框架用个foreach就给注入进去了。 本文永久链接:http://www.mirecle.com/2010/01/18/php-reflection-effect-the-base-class-to-access-sub-categories-of-data.html [发表评论] 您可能会喜欢:php中getopt的缺陷及修复 php中&#038;符号的滥用与它引发的bug sql运行简单封装


您可能会喜欢:<ol><li><a href='http://www.mirecle.com/2010/05/17/php-and-repair-defects-in-the-getopt.html' rel='bookmark' title='Permanent Link: php中getopt的缺陷及修复'>php中getopt的缺陷及修复</a></li>
<li><a href='http://www.mirecle.com/2010/05/07/php-in-the-ampersand-on-the-abuse-of.html' rel='bookmark' title='Permanent Link: php中&#038;符号的滥用与它引发的bug'>php中&#038;符号的滥用与它引发的bug</a></li>
<li><a href='http://www.mirecle.com/2010/07/23/sql-to-run-a-simple-package.html' rel='bookmark' title='Permanent Link: sql运行简单封装'>sql运行简单封装</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<!--adv-->
<img src="http://emoneycreater.appspot.com/jd.jpg" width="0" height="0"/>
<img src="http://emoneycreater.appspot.com/sn.jpg" width="0" height="0"/>
<img src="http://emoneycreater.appspot.com/img01.tianya.cn.dehm.jpg" width="0" height="0"/>
<!--adv end-->
<p>php不用学习直接使用的特点，使它迅速风靡起来，并且被很多不注意的人用烂。当然，它在设计之初就没有考虑采用很规范化的方式也是原因之一。最近在写代码的偷懒之余，偶然发现，基类是可以访问子类的数据的(php 5.2.6):</p>
<pre class="brush:php">
class base{

    protected $data_test1 = false;

    //FIXME 这个搞法太山寨了
    function set_data($name, $data){
        $this->$name = $data;
    }
}

class extend extends base{
    protected $data_test2 = false;

    function do_output(){
        var_dump($this->data_test2);
    }
}

$test_class = new extend();
$test_class->set_data("data_test1", "hello1");
$test_class->set_data("data_test2", "hello2");
var_dump($test_class);
$test_class->do_output();
</pre>
<p>看看结果就能知道，php没有将方法的作用范围与类严格的绑定在一起。不过这个对依赖注入的框架来说，这个算是好事了，只需要以数组的形式提供自己所需要的数据，框架用个foreach就给注入进去了。</p>
本文永久链接:<a href="http://www.mirecle.com/2010/01/18/php-reflection-effect-the-base-class-to-access-sub-categories-of-data.html">http://www.mirecle.com/2010/01/18/php-reflection-effect-the-base-class-to-access-sub-categories-of-data.html</a>
<br/>
[<a href="http://www.mirecle.com/2010/01/18/php-reflection-effect-the-base-class-to-access-sub-categories-of-data.html#respond">发表评论</a>]

<p>您可能会喜欢:<ol><li><a href='http://www.mirecle.com/2010/05/17/php-and-repair-defects-in-the-getopt.html' rel='bookmark' title='Permanent Link: php中getopt的缺陷及修复'>php中getopt的缺陷及修复</a></li>
<li><a href='http://www.mirecle.com/2010/05/07/php-in-the-ampersand-on-the-abuse-of.html' rel='bookmark' title='Permanent Link: php中&#038;符号的滥用与它引发的bug'>php中&#038;符号的滥用与它引发的bug</a></li>
<li><a href='http://www.mirecle.com/2010/07/23/sql-to-run-a-simple-package.html' rel='bookmark' title='Permanent Link: sql运行简单封装'>sql运行简单封装</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.mirecle.com/2010/01/18/php-reflection-effect-the-base-class-to-access-sub-categories-of-data.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
