MVC框架入门准备(一)

最近开发一套自己的框架,开发了一个多月,已传到git,学到了挺多,系列文章我会慢慢写挺多

这里先讲大致框架多用到的某些函数,我这里先列举一部分,网上对函数的定义,参数说明等,我就不照搬了,记录自己的理解和测验:

一  get_called_class

谁调用就显示调用的类名(包括继承等),框架中用到了很多,在创建驱动等方面用到了很多。

举例:

class foo {
    static public function test() {
        var_dump(get_called_class());
    }
}
class bar extends foo {
}
foo::test();
bar::test();
输出:
string(3) "foo"
string(3) "bar"

二   func_get_args

获取真实传递的参数,不要忽略了,有些函数有默认值的,这些不算在func_get_args内,在框架中用到的也颇多。主要用于未知用户键入的参数以及多变化的处理情况。

create($name, $type = 'php'){
Var_dump(func_get_args());
}

create('@'); ==》
输出array
  0 => string '@' (length=1)
create(
'@',’dd’); ==> array 0 => string '@' (length=1) 1 => string 'dd' (length=2)

三   call_user_func_array

— 调用回调函数,并把一个数组参数作为回调函数的参数

<?php
function foobar($arg, $arg2) {
    echo __FUNCTION__, " got $arg and $arg2\n";
}
class foo {
    function bar($arg, $arg2) {
        echo __METHOD__, " got $arg and $arg2\n";
    }
}

call_user_func_array("foobar", array("one", "two"));

$foo = new foo;
call_user_func_array(array($foo, "bar"), array("three", "four"));
?>  

以上例程的输出类似于:

foobar got one and two
foo::bar got three and four

如你所见,通过载入不同的array配置可以回调不同的方法,可以用于某些事件监听与触发之间

四    ReflectionClass 反射

这当然是一个最为重要的部分。在框架调度的过程中,经常用到反射

//类测试与反射
    class testExtend{
        public function sikao(){
            //echo 'I am thinking ......';
        }

    }
    class test extends testExtend{
        public function __construct()
        {
            echo "i am construct ....";
        }
        const conn = 'nihao';
        const conn1 = 'nihao';
        public $nl = 0;
        public $nl2 = 10;
        
        public function addNl(){
            $this->nl = 10;        
        }
        public function say(){
            echo 'i am say...';
        }
        public function sikao(){
            echo 'i am thinking......';
        }
    }

$ref = new ReflectionClass('test' );   //参数用类名也可以用对象
    var_dump($ref);
    getClass($a);


    function getClass($className){
        echo '<pre>';
        $ref = new ReflectionClass($className);
        echo 'class name is : '.$ref->getName().'<br><br>';
        //获取常量
        $contans = $ref->getConstants();
        echo 'the contans is : ';
        foreach ($contans as $k => $v) {
             echo $k . ' : ' . $v . ' || ';
        }
        echo '<br>','<br>';

        //获取属性
        $shuxing = $ref->getProperties();
        foreach ($shuxing as $k => $v) {
            foreach ($v as $i => $j) {
                if( 'name' == $i){
                    echo 'the '. $k .' shuxing is : '.$j .'<br>';
                }
            }
        }
        echo '<br>';

        //获取方法
        $mothods = $ref->getMethods();
        foreach ($mothods as $k => $v) {
            foreach ($v as $i => $j) {
                if( 'name' == $i){
                    echo 'the '. $k .' methods is : '.$j .'<br>';
                }
            }
        }
    }

 五  __callStatic && __call

__call 当要调用的方法不存在或权限不足时,会自动调用__call 方法。

__callStatic 当调用的静态方法不存在或权限不足时,会自动调用__callStatic方法。

/**
     * 魔术方法
     *
     * @param type $name            
     * @param type $arguments            
     * @return type
     */
    public static function __callStatic($name , $arguments ) //$name 为调用方法名,$arguments 为参数
    {
        //$name = get
        // var_dump($arguments);array
        // 0 => string 'm' (length=1)
        // 1 => boolean false
        return call_user_func_array('self::getAll', array_merge(array ($name), $arguments));
    }

 

数据操作:

六 ArrayAccess(数组式访问)接口 :像数组一样来访问你的PHP对象

复制代码
class Test implements ArrayAccess {
            private $elements;
            
            public function offsetExists($offset){
                return isset($this->elements[$offset]);
            }
            
            public function offsetSet($offset, $value){
                $this->elements[$offset] = $value;
            }
            
            public function offsetGet($offset){
                return $this->elements[$offset];
            }
            
            public function offsetUnset($offset){
                unset($this->elements[$offset]);
            }
        }
        $test = new Test();
        $test['test'] = 'test';//自动调用offsetSet
        if(isset($test['test']))//自动调用offsetExists
        {
            echo $test['test'];//自动调用offsetGet
            echo '<br />';
            unset($test['test']);//自动调用offsetUnset
            var_dump($test['test']);
        }
复制代码

七 数据连贯操作举例

复制代码
class test{
        public $nl = 0;
        public $nl2 = 10;
        
        public function say(){
            $this->nl = 100;
            return $this;  //重点在于返回当前类实例
        }
        public function sikao(){
            echo $this->nl;
        }
    }

    $a = new test();
    $a->say()->sikao();
复制代码

 

闭包

 1 匿名函数

提到闭包就不得不想起匿名函数,也叫闭包函数(closures),貌似PHP闭包实现主要就是靠它。声明一个匿名函数是这样:

复制代码
$func = function() {

}; //带结束符

可以看到,匿名函数因为没有名字,如果要使用它,需要将其返回给一个变量。匿名函数也像普通函数一样可以声明参数,调用方法也相同: 代码如下:
$func = function( $param ) {
    echo $param;
};
$func( 'some string' );
//输出:
//some string
复制代码

2 闭包

将匿名函数在普通函数中当做参数传入,也可以被返回。这就实现了一个简单的闭包。

复制代码
/例一 //在函数里定义一个匿名函数,并且调用它
function printStr() {     $func = function( $str ) {         echo $str;     };     $func( 'some string' ); } printStr();
复制代码
复制代码
//例二
//在函数中把匿名函数返回,并且调用它
function getPrintStrFunc() {
    $func = function( $str ) {
        echo $str;
    };
    return $func;
}
$printStrFunc = getPrintStrFunc();
$printStrFunc( 'some string' );
复制代码
复制代码
//例三
//把匿名函数当做参数传递,并且调用它
function callFunc( $func ) {
    $func( 'some string' );
}
$printStrFunc = function( $str ) {
    echo $str;
};
callFunc( $printStrFunc );
//也可以直接将匿名函数进行传递。如果你了解js,这种写法可能会很熟悉
callFunc( function( $str ) {
    echo $str;
} );
复制代码

3 连接闭包和外界变量的关键字:USE【重点】

闭包可以保存所在代码块上下文的一些变量和值。PHP在默认情况下,匿名函数不能调用所在代码块的上下文变量,而需要通过使用use关键字。

换一个例子看看:

复制代码
function getMoney() {
    $rmb = 1;
    $dollar = 6;
    $func = function() use ( $rmb ) {
        echo $rmb;
        echo $dollar;
    };
    $func();
}
getMoney();
//输出:
//1
//报错,找不到dorllar变量
复制代码

 

posted @ 2017-04-18 00:10  hongxinerke  阅读(515)  评论(0编辑  收藏  举报