PHP - 如何能够在 __autoload() 函数中使用变量

发布于 2024-09-13 10:43:19 字数 1418 浏览 3 评论 0原文

我正在使用的自动加载函数如下:-

function __autoload($moduleName) 
{
     //Logic to check file existence- include if exists else redirect to fallback page
}

它不需要任何其他参数吗?我想根据自动加载函数内的一些变量执行一些逻辑。在不使用全局变量的情况下如何做到这一点?

谢谢

其他详细信息

我认为这在 __autoload() 类中是不可能的,但仍在尝试用示例进行解释。

我有一个包含数组的 module.config 文件:-

$viewClassMap =  array('search_classes' => 'commonClassListings',
                       'search_packs' => 'commonPackListings',
                       );

上面的数组意味着在 search_classes 视图中必须包含类 commonClassListings,在 search_packs 视图中必须包含类 commonPackListings。对于所有其他视图,默认情况下,将包含 commonDisplay 类

function __autoload($viewName,$viewClassMap) 
{
    if(in_array($viewName,$viewClassMap))
    {
       $viewTobeIncluded = $viewClassMap[$viewName];

       include path/to/$viewTobeIncluded;   
    }
    else
    {
       include path/to/commonDisplay;   
    }
}

现在,我认为 __autoload 函数内部的逻辑必须移出,首先必须计算要加载的视图,然后只需要调用自动加载。这也是自动加载的目的(包括启动其对象的类文件)。

更新

我的自动加载函数不在我将在运行时启动其对象的视图类内部(与 Blizz 的示例不同)。我在 pageLoader.php 文件中定义了一个通用自动加载函数,该文件包含在通用前端控制器中。对于每个模块,都会启动视图类,

$view  = new search_classes();

然后通用自动加载函数必须检查相应的父视图类(在本例中为 commonClassListings)是否存在,如果存在,则包含它和 search_classes 视图本身并启动对象,否则回退。为此,我需要将 $viewClassMap 数组传递给自动加载函数。这可能吗?

The autoload function I am using is as follows:-

function __autoload($moduleName) 
{
     //Logic to check file existence- include if exists else redirect to fallback page
}

Does it not take any other arguments? I want to perform some logic based on some variables inside the autoload function. How do I do it without the use of global variables?

Thanks

Additional Details

I think this is not possible inside __autoload() class but still trying to explain with an example.

I have a modules.config file which has an array:-

$viewClassMap =  array('search_classes' => 'commonClassListings',
                       'search_packs' => 'commonPackListings',
                       );

The above array means that class commonClassListings has to be included in case of search_classes view, class commonPackListings has to be included in case of search_packs view. For all other views, by default, commonDisplay class will be included

function __autoload($viewName,$viewClassMap) 
{
    if(in_array($viewName,$viewClassMap))
    {
       $viewTobeIncluded = $viewClassMap[$viewName];

       include path/to/$viewTobeIncluded;   
    }
    else
    {
       include path/to/commonDisplay;   
    }
}

Now, I think the logic inside __autoload function has to be moved out, and first the view to be loaded has to be computed and then only autoloading has to be called. That is the purpose of autoloading also (to include the class file whose object was initiated).

Updates

My autoload function is not inside the view class whose object I am going to initiate at run time (Unlike in Blizz's example). I have a common autoload function defined in a pageLoader.php file which is included in the common froncontroller. For every module, the view class is initatiated like

$view  = new search_classes();

Then the common autoload function has to check the corresponding parent view class (commonClassListings in this case) is present or not, if so include it and the search_classes view itself and iniate the object, else fallback. For this I need to pass the $viewClassMap array to the autoload function. Is that possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

空‖城人不在 2024-09-20 10:43:20

我能想到的唯一方法是将类对象注册为自动加载处理程序(使用 spl_autoload_register)。您可以随时通过指定 array($oInstance, "functionName") 作为回调来注册实例。

在执行触发自动加载的操作之前,您可以在该类实例上设置变量。

编辑:
根据您的要求,提供一个有关其工作原理的小示例。
请注意,我通常在类中使用静态方法来进行自动加载,因此我不知道它是否适用于实例,但我检查了文档,它应该适用。如果不起作用,您可以将函数和数组设为静态

class Test
{
    public $viewClassMap =  array('search_classes' => 'commonClassListings',
                       'search_packs' => 'commonPackListings',
                       );

   public function autoload($viewName)
   {
    if (in_array($viewName,$this->viewClassMap))
            ...
    }
}

$test = new Test;
spl_autoload_register(array($test, 'autoload'));

The only way I can think of is registering a class object as autoload handler (with spl_autoload_register). You can register an instance at any time by specifying array($oInstance, "functionName") as the callback.

You would be able to set variables on that class instance before you do the operation that will trigger the auto load.

Edit:
As per your request a small example on how this works.
Note that I usually work with static methods in a class for the autoload so I don't know if it works with an instance, but I checked the documentation and it should. You can just make the function and the array static if it doesn't work

class Test
{
    public $viewClassMap =  array('search_classes' => 'commonClassListings',
                       'search_packs' => 'commonPackListings',
                       );

   public function autoload($viewName)
   {
    if (in_array($viewName,$this->viewClassMap))
            ...
    }
}

$test = new Test;
spl_autoload_register(array($test, 'autoload'));
灼痛 2024-09-20 10:43:20

您没有调用 __autoload 函数,因此向其传递参数没有意义。如果您想保留其中的某些状态(例如跟踪所有加载的类/计时或其他),您可以使用静态变量。

你能告诉我们更多关于你想要做的逻辑吗?也许还有更好的方法。

You do not call the __autoload function so passing parameters to it doesn't make sense. You can use static variables if you want to preserve some state in it (e.g. tracking all loaded classes / timings or whatever).

Can you tell us more about the logic you want to do? May be there are better ways.

牛↙奶布丁 2024-09-20 10:43:20

想法是,使用一个常量。至少努力解决我的问题。

当然,每个文件只能定义一次常量,并且您必须根据您的情况调整以下代码(即 $dir$viewClassMap)。

这就是我如何将另一个“参数”传递给自动加载:

define('DIR',$dir);

function foo_autoload($class){
    $packages=array('pack','pack2');

    foreach($packages as $v){
        if(file_exists(DIR.$v.'/'.$class.'.php')==true){
            require(DIR.$v.'/'.$class.'.php');
            break;
        }
    }
}
spl_autoload_register('foo_autoload');

如果用变量定义常量似乎没有意义,那么如果该变量来自方法的参数,则确实如此。在我的例子中,所有代码都在另一个方法中。

The ideia is, use a constant. At least worked to solve my problem.

You can define the constant only once per file, of course, and you will have to adapt the following code to your situation (i.e. $dir to $viewClassMap).

That's how i could pass another "param" to the autoload:

define('DIR',$dir);

function foo_autoload($class){
    $packages=array('pack','pack2');

    foreach($packages as $v){
        if(file_exists(DIR.$v.'/'.$class.'.php')==true){
            require(DIR.$v.'/'.$class.'.php');
            break;
        }
    }
}
spl_autoload_register('foo_autoload');

If doesn't seems to make sense define a constant with a variable, it does, if that variable comes from a param of a method. All that code is inside another method in my case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文