js获取地址栏get参数

//获取url中get参数的值
function getQuery(name){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
      hash = hashes[i].split('=');
      vars.push(hash[0]);
      vars[hash[0]] = hash[1];
    }
    if('undefined' != vars[name]){
    	return vars[name];
    }else{
    	return '';
    }
}
//获取url中"?"符后的字串
function getQueryString() {
	var url = location.search;
	var str="";
	if (url.indexOf("?") != -1) {
		var str = url.substr(1);
	}
	return str;
}

 

Java进程超时控制

可以使用daemon线程实现,因为daemon在没有其他线程运行后会自动结束。

这样如果设置了100秒超时,但是所有线程(包括主线程)只运行了10秒就不会产生任何问题。

Thread threadExpire = new Thread() {
	public void run() {
		try {
			sleep(expire * 1000);
		} catch (Throwable e) {
			Run.catchException(this, e);
		} finally {
			// 执行到这里肯定是超时了
			Run.catchException(
					this,
					new Exception(
							obj.getClass()
									.getName()
									+ " has expired,expire="
									+ expire));
			System.exit(0);
		}
	}
};
threadExpire.setDaemon(true);
threadExpire.start();

 

Yaf中PHPUnit单元测试最佳实践

public同级目录建立tests目录

tests/phpunit.xml

<phpunit bootstrap="./index.php">
	<testsuites>
		<testsuite name="controllers">
			<file>./controllers/TestIndex.php</file>
		</testsuite>
	</testsuites>
</phpunit>

tests/index.php

<?php
define ( 'APP_PATH', dirname ( __DIR__ ) . '/app', true );
(new Yaf_Application ( APP_PATH . '/conf/app.ini' ))->bootstrap ();

tests/controllers/TestIndex.php

class TestIndex extends PHPUnit_Framework_TestCase {
	public function testIndex() {
		$request=new Yaf_Request_Simple('CLI','','Index','test');
		$res=Yaf_Application::app()->getDispatcher()->returnResponse(true)->dispatch($request);
		$valid='test string';
		$this->assertEquals ( $valid, $res->getBody());
	}
}

APP_PATH.'/controllers/Index.php'

class IndexController extends Yaf_Controller_Abstract {
	function testAction(){
		$this->getResponse()->setBody('test string');
	}
}

运行

[root@ares tests]# phpunit 
PHPUnit 4.8.6 by Sebastian Bergmann and contributors.

.

Time: 105 ms, Memory: 4.00Mb

OK (1 test, 1 assertion)

 

参考:

http://www.crackedzone.com/phpunit-yaf-controller.html

PHP吊炸天的单例模式,世界一流

<?php
trait Trait_Singleton {
	private static $instances = array ();
	/**
	 *
	 * @return self
	 */
	static function getInstance() {
		$name = get_called_class ();
		$args = func_get_args ();
		$key = $name . '-' . Helper_ArrayObject::implode ( '', $args );
		if (! isset ( self::$instances [$key] )) {
			$reflection = new ReflectionClass ( $name );
			self::$instances [$key] = $reflection->newInstanceArgs ( $args );
		}
		return self::$instances [$key];
	}
}

无所不能的单例模式,无视继承和构造方法任意参数。

github

https://github.com/ares333/php-yaf-library/blob/master/src/Singleton.php