dongzhuang6417 2010-07-27 18:58
浏览 78
已采纳

php:在可选文件中多次运行带有一次性加载类的函数

after reading the responses I have rewritten my question.

Let's say I have a theoretical php application that uses objects that do different things. For every page that gets loaded by the application, different scripts will be run.

now I made a simple php script that creates a simple object. (this is all made up, I'm just trying to understand this before I code it)

$user = new userClass($db);
$user->login($credentials);

all is fine, and I can even repeat the procedure several times after creating the user class

$user = new userClass($db);
$user->login($credentials);
...
$user->login($credentials2);

now imagine if these 2 parts are split in 2 different php files.

file1:
$user = new userClass($db);
$user->login($credentials);
file2:
$user->login($credentials2);

include file1
include file2

if they are run in order all is fine, but if they are not, or if file1 is not included at all...

file2:
$user->login($credentials);
file1:
$user = new userClass($db);
$user->login($credentials2);

include file2
include file1

then it won't work... they have to maintain order, so lets make a main php file that gets loaded no matter what.

main file:
$user = new userClass($db);
file1:
$user->login($credentials);
file2:
$user->login($credentials2);

(included files with autoloading for example) include main include file1 include file2 or include main include file2 include file1 or include main include file2

now file1 and 2 can be loaded in any order, and one does not depend on the other, but what if both file1 or file2 are unnecessary?

main file:
$user = new userClass($db);
//nothing else gets loaded

then the main file is also unnecessary, and will prove to be a resource hog, sure if its one class it's no problem, but what if the main file loads hundreds of classes that never get used?

not elegant at all.

Then let's try to do it another way, let's scrap the main file completely and do it like this (below is the bare bone example of what I want to achieve):

file1:
if(!is_object($user)){
    $user = new userClass($db);
}
$user->login($credentials);
file2:
if(!is_object($user)){
    $user = new userClass($db);
}
$user->login($credentials2);

sure, it works, but it's not elegant, now is it?

let's try it with method overloading...

class loader {
   private $objects;
   private $db;
   function __construct($db){
      $this->objects = array();
      $this->db = $db;
   }
   function __get($name){
      if(!isset($this->objects[$name])){
         return $this->objects[$name] = new $name($this->db);
      }
   }
}

main file:
$loader = new loader($db);
file1:
$loader->user->login($credentials);
file3:
$loader->user->login($credentials3);
file2:
$loader->user->login($credentials2);
file4:
$loader->user->login($credentials4);

seems to work, until you realize that you can no longer give any of the objects that are created this way any other variable except $db. This means that loader class is limited to use with user class only (for example) because using loader class with any other class will require editing of the loader class

and a simple script like this:

$user = new userClass($this->db);
$user->login($credentials);
$form = new formClass($_POST);
$form->process($info);

will require 2 separate loader classes or at least 2 methods in the loader class

   class loader {
       private $objects;
       private $db;
       function __construct($db){
          $this->objects = array();
          $this->db = $db;
       }
       function getuserclass($name){
          if(!isset($this->objects[$name])){
             return $this->objects[$name] = new $name($this->db);
          }
       }
       function getformclass($name){
          if(!isset($this->objects[$name])){
             return $this->objects[$name] = new $name($_POST);//just an unrealistic example.
          }
       }
    }

main file:
$loader = new loader($db);
file1:
$loader->getuserclass('user')->login($credentials);
file3:
$loader->getformclass('form')->process($info);

but this is not elegant either.

How is this really supposed to be done?

  • 写回答

3条回答 默认 最新

  • dongrang2140 2010-07-27 19:04
    关注

    This would probably work for you:

    class loader {
       private $objects;
    
       function __construct(){
          $this->objects = array();
       }
    
       function __call($name, $args){
          if(!isset($this->objects[$name])){
             return $this->objects[$name] = new $name($args);
          }
       }
    }
    $loader = new loader();
    
    $user = $loader->user($db);
    $form = $loader->form($form_args);
    

    If you want multiple instances you could perhaps include an additional first argument in each call to loader that is a name, and change the loader code to use the first arg as a key for the object, and pass the rest to the object's constructor.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行