0

我正在制作自己的框架,它具有创建类的功能。如果 classA 有一个名为 classes 的属性,则意味着 classA 需要 classes 属性中的任何类,该属性是 key=className 和 value=classInstance 的数组。

即,如果加载了此类,它将创建 foo 和 biz 实例并用它们替换 null

class foo
{
    public $classes=array('bar'=>null, 'biz'=>null);
}

如果只有一个依赖项,我的框架可以正常工作,但是当有更多依赖项时,只有数组中的最后一个能成功加载。其余的都设置为 false =/ 这令人费解,因为它应该设置一条消息到带有错误的属性,但它不是......

无论如何,这里是处理加载和错误的主类,创建类的方法是 create()。

<?php
session_start();
class yamiko 
{
    public $paths=array
            (
        'php'=>'_php/',
        'js'=>'_js/',
        'css'=>'_style/',
        'template'=>'_template/'
            );
    public $url=null;#defined in constructor
    public $classes=array();
    private $errors=array();
    private $warnings=array();

    /*
     * define $this->url
     * @RETURN: none
     */
    public function __construct() 
    {
        $this->url=urlencode($_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]);
    }

    ###################
    ##### CLASSES #####
    ###################

    /*
     * create a class instance
     * @RETURN OBJECT||false: returns reference to object of false if failed 
     * @PARAM $class STRING: path and class name.
     */
    public function &create($class, $php=false, $c=0)
    {
        #check if php class
        if($php)
        {
            #make instance
            if(!$this->get_class($class))
            {
                $this->classes[$class]=new $class;
            }
            return $this->classes[$class];
        }else#non php class
        {
            $path=$class;
            $pos=strrpos($path, '/');
            $class=substr($path, $pos+1);
            $path=substr($path, 0, $pos+1);
            if($this->get_class($class))
                return $this->classes[$class];#return existing class
            if(file_exists($path.$class.'.php'))
            {
                include_once($path.$class.'.php');
            }  else
            {
                $this->set_error("unable to include $path$class");
                return false;
            }
            $this->classes[$class]=new $class;
            #get required classes for class
            if(property_exists($this->classes[$class], 'classes'))#if class has a classes property
            {
                foreach($this->classes[$class]->classes as $k=>$v)
                {
                    $required_class=$this->get_class($k);#check if it already exist
                    if(!$required_class)
                    {
                        $required_class=&$this->create($path.$k, false, $c+1);
                        if(!$required_class)
                        {
                            $this->set_error("Class $class requires class $k");
                            return FALSE;
                        }
                    }
                    $this->classes[$class]->classes[$k]=&$required_class;
                }
            }
            return $this->classes[$class];
        }
    }

    /*
     * gets a references to a class or returns false
     * @RETURN OBJECT||false: returns reference to class or false if not found
     */
    public function &get_class($class)
    {
        if($this->classes[$class] instanceof $class)
            return $this->classes[$class];
        $this->set_warn("$class does not exist");
        return false;
    }

    ###################
    ##### ERRORS ######
    ###################

    public function set_error($e)
    {
        $this->errors[]=$e;
    }

    public function get_error($r)
    {
        if(empty($this->errors))
            return false;
        switch ($r)
        {
            case 'html':
                $html='<ul>';
                foreach($this->errors as $k=>$v)
                {
                    $html.="<li><b>ERROR $k:</b> $v </li>";
                }
                $html.='</ul>';
                return $html;
            break;
            default:#array
                return $this->errors;
                break;
        }
    }

    ####################
    ##### WARNINGS #####
    ####################

    public function set_warn($w)
    {
        $this->warnings[]=$w;
    }

    public function get_warn($r)
    {
        if(!empty($this->warnings))
            return false;
        switch ($r)
        {
            case 'html':
                $html='<ul>';
                foreach($this->warnings as $k=>$v)
                {
                    $html.="<li><b>WARNING $k:</b> $v </li>";
                }
                $html.='</ul>';
                return $html;
            break;
            default:#array
                return $this->warnings;
                break;
        }
    }

    ################
    ##### DEBUG ####
    ################

    public function debug($msg, $r)
    {
        if(!is_string($msg))
        {
            $this->set_error('yamiko->debug msg is not a string');
            return false;
        }
        if($msg!='li'&&$msg!='br'&&$msg!='h1'&&$msg!='h2'&&$msg!='h3')
        {
            $this->set_error('yamiko->debug PARAM $r must be li, br, h1, h2, or h3');
            return false;
        }   
        switch($r)
        {
            case 'li':
                echo "<li>$msg</li>";
                break;
            case 'br':
                echo "$msg<br />";
                break;
            case 'h1':
                echo "<h1>$msg</h1>";
                break;
            case 'h2':
                echo "<h2>$msg</h2>";
                break;
            case 'h3':
                echo "<h3>$msg</h3>";
                break;
        }
    }
}
global $yamiko;
$yamiko=new yamiko;
$GLOBALS['yamiko']=&$yamiko;
?>

我试图加载的类设置如下,只有第一个类加载。我切换了顺序,再次只加载第一类。

<?php
class account
{
    public $classes=array
    (
'yamiko_mysqli'=>NULL,
'yamiko_encrypt'=>NULL
    );
}
?>
4

0 回答 0