php快速记录日志的函数,接收两个参数,日志内容与日志等级。
<?php
/**
* record logs file
* @param <type> $lvevel
* @param string $level
* @http://www.
*/
define("YUC_LOG_TYPE", "1,2,3,4,5,6"); //日志级别
define("M_PRO_DIR", "./"); //日志目录
define("FILE_APPEND", 1); //是否追加
function Write($msg, $level) {
$arr_level = explode()(',', YUC_LOG_TYPE);
if (in_array($level, $arr_level)) {
$record = date('Y-m-d H:m:s') . " >>> " . number_format(microtime(TRUE), 5, ".", "") . ' ' . " : " . $level . "\t" . $msg;
$base = M_PRO_DIR . "/Log";
$dest = $base . "/" . date("YmdH", time()) . 'log.php';
if (!file_exists($dest)) {
@mkdir($base, 0777, TRUE);
@file_put_contents($dest, "<?php die('Access Defined!');?>\r\n", FILE_APPEND);
}
if (file_exists($dest)) {
@file_put_contents($dest, $record . "\r\n", FILE_APPEND);
}
}
}
//调用示例
$msg = "www.---access logs--testing---";
write($msg, 2);
?>
执行后的结果,如下图所示:
在当前目录下,创建Log目录,并在其中生成日志文件。
php设计模式之工厂模式的例子。
<?php
/**
*php设计模式 工厂模式
*site http://www.
*/
class FruitFactory {
private $history, $class, $constructor_args;
/**
* Create a factory of given class. Accepts extra arguments to be passed to
* class constructor.
*/
function __construct( $class ) {
var_dump($args = func_get_args());
$this->class = $class;//类名
$this->constructor_args = array_slice( $args, 1 );//参数
}
function __call( $method, $args ) {
$this->history[] = array(
'action' => 'call',
'method' => $method,
'args' => $args
);
var_dump($this->history);
}
function __set( $property, $value ) {
$this->history[] = array(
'action' => 'set',
'property' => $property,
'value' => $value
);
var_dump($this->history);
}
/**
* Creates an instance and performs all operations that were done on this MagicFactory
*/
function instance() {
# use Reflection to create a new instance, using the $args
$reflection_object = new ReflectionClass( $this->class );
$object = $reflection_object->newInstanceArgs( $this->constructor_args );
foreach( $this->history as $item ) {
if( $item['action'] == 'call' ) {
//运行实例的方法
call_user_func_array( array( $object, $item['method'] ), $item['args'] );
}//属性赋值
if( $item['action'] == 'set' ) {
$object->{$item['property']} = $item['value'];
}
}
# Done
return $object;
}
}
class Fruit {
private $name, $color;
public $price;
function __construct( $name, $color ) {
$this->name = $name;
$this->color = $color;
}
function setName( $name ) {
$this->name = $name;
}
function introduce() {
print "Hello, this is an {$this->name} {$this->color}, its price is {$this->price} RMB.";
}
}
# Setup a factory
$fruit_factory = new FruitFactory('Fruit', 'Apple', 'Gonn');
$fruit_factory->setName('Apple');
$fruit_factory->price = 2;
# Get an instance
$apple = $fruit_factory->instance();
$apple->introduce();
?>
php设计模式之单例模式的例子。
<?php
/**
* php设计模式 单例模式
* site http://www.
*/
class Fruit {
private static $instanceMap = array();
//protected getter for singleton instances
protected static function getSingleton($className)
{
//保证单例模式 并且不能从控制器实例化和克隆
if(!isset()(self::$instanceMap[$className]))
{
$object = new $className;
//Make sure this object inherit from Singleton
if($object instanceof Fruit)
{
self::$instanceMap[$className] = $object;
var_dump($object);
}
else
{
throw SingletonException("Class '$className' do not inherit from Singleton!");
}
}
return self::$instanceMap[$className];
}
//protected constructor to prevent outside instantiation
protected function __construct(){
}
//denie cloning of singleton objects
public final function __clone(){
trigger_error('It is impossible to clone singleton', E_USER_ERROR);
}
}
class Apple extends Fruit {
protected $rndId;
public function __construct(){
$this->rndId = rand();
}
public function whatAmI(){
echo 'I am a Apple('.$this->rndId.')<br />';
}
public static function getInstance(){
//echo get_class();
return Fruit::getSingleton(get_class());
}
}
class GreenApple extends Apple {
public function whatAmI(){
echo 'I am a GreenApple('.$this->rndId.')<br />';
}
public static function getInstance(){
return Fruit::getSingleton(get_class());
}
}
$apple1 = Apple::getInstance();
//var_dump($apple1);
$apple2 = GreenApple::getInstance();
$apple1->whatAmI();// should echo 'I am a Apple(some number)
$apple2->whatAmI();// should echo 'I am a GreenApple(some number)
$apple1 = Apple::getInstance();
$apple2 = GreenApple::getInstance();
//保证单例模式
$apple1->whatAmI();// should echo 'I am a Apple(same number as above)
$apple2->whatAmI();// should echo 'I am a GreenApple(same number as above)
// $a = clone $apple1;// this should fail
// $b = clone $apple2;// this should fail
?>
您可能感兴趣的文章:
php单例模式为何只能实例化一次
学习php设计模式之单例模式
php实现的单例模式的例子
学习php单例模式及应用实例
php单例模式的演示代码
有关php单例模式介绍及例子
php设计模式之单例模式学习
php单例模式的例子