码迷,mamicode.com
首页 > Web开发 > 详细

MVC框架入门准备(一)

时间:2017-04-18 00:41:29      阅读:297      评论:0      收藏:0      [点我收藏+]

标签:bsp   例程   nihao   框架   got   bar   boolean   过程   调用   

最近开发一套自己的框架,开发了一个多月,已传到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));
    }

 

MVC框架入门准备(一)

标签:bsp   例程   nihao   框架   got   bar   boolean   过程   调用   

原文地址:http://www.cnblogs.com/zhenghongxin/p/6725762.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!