gubnota / smart_loader
在不需要每次在新的文件中创建新的类实例的情况下,更简单直接地调用伪静态方法
Requires
- php: >=5.4
This package is not auto-updated.
Last update: 2024-09-24 02:11:39 UTC
README
Gubnota/smart_loader (别名 g::
或 \Gubnota\Gubnota
) 是一个类助手,用于加载和重用类实例。它使得在整个应用程序中重用类实例变得更加容易(无需重新初始化新实例,每个实例都存储在全局对象中)。通过在伪静态方式调用类方法时重用实例,自动加载类助手可以极大地提高性能和便利性。
<?php g::Class('method','arg',...); //or g::instance()->Class->method('arg',...); ?>
你可以像jQuery一样传递变量到其他调用者(方法)。默认情况下,g::
仅存储一个类实例,并在从其他地方调用相同类时重用它。自动加载器可以用两种方式使用:composer pakagist默认psr-4自动加载器和g::
自己的自动加载器。完全兼容 >= PHP 5.4,PHP 7.0。
通过composer安装
将gubnota/smart_loader
字符串添加到您的composer.json的require部分,并运行composer update
"require": { "php" : ">=5.4", "gubnota/smart_loader": "dev-master" },
或者,您可以根据您的自动加载模式子系统下载包
<?php include('smart_loader-master/init.php'); ?>
包含的文件
- test.php - 使用示例
- g.php - 类名称简写(调用g::而不是Gubnota\Gubnota)
- Gubnota/Gubnota.php - 执行所有操作的主要类助手
- John/Doe.php - 调用类(只有一个公共方法)
- init.php - 包含此文件来自动加载
自动加载类
如果您不打算使用Composer psr-4标准自动加载类解决方案,请务必设置自己的
<?php include(__DIR__.'/vendor/gubnota/smart_loader/init.php'); ?>
您可能希望通过添加额外的目录来搜索更高级的使用
<?php include(__DIR__.'/vendor/gubnota/smart_loader/Gubnota/Smart_loader.php'); $l = \Gubnota\Smart_loader::instance(); $l->place('name_for_new_place_to_look_for',__DIR__); spl_autoload_register([$l, 'load']); ?>
当您在其他地方调用smart_autoloader时
重命名长名称
smart_loader还能将长类名重命名为短名称
<?php g::instance()->facade('john','John\Doe')->get_instance('john'); ?>
请参阅外观部分。
使用方法
仅创建一个用于调用单个方法的类实例
<?php $john_doe = new John\Doe(); $john_doe->write_message_to('Samuel Smitters'); ?>
这通常是一个头疼的问题:每次在新的文件中使用该类方法时,都必须创建它。旧方法包括利用静态方法或创建全局变量。新的方法包含在Laravel 4中,例如,使用catch name方法创建实例,将每个调用的类实例保存到特殊数组中,以便再次调用而无需重新创建。在大多数情况下,您只需要这种方法。
以下几种使用Gubnota/Gubnota加载器初始化和调用相同类方法的相同方式
<?php \Gubnota\Gubnota::John('write_message_to', 'Laura Smith', 'Are you still there?'); ?>
或
<?php \Gubnota\Gubnota::instance()->John->write_message_to('Dude', 'Got message?'); ?>
或
<?php \Gubnota\Gubnota::instance()->John->write_message_to('Dude II', 'Got message?'); ?>
或
<?php g::{'John\\Doe'}('write_message_to', ['Laura Smith','John Smith','John Marcus','Mark Brown'][rand(0,3)], ['How long does it take to reach your house?','How about we meet next Tuesday?','Hello there.','Are you still there?'][rand(0,3)]); ?>
或
<?php g::instance()->{'John\\Doe'}->write_message_to( ['Laura Smith','John Smith','John Marcus','Mark Brown'][rand(0,3)], ['How long does it take to reach your house?','How about we meet next Tuesday?','Hello there.','Are you still there?'][rand(0,3)]); ?>
或
<?php $instance = \Gubnota\Gubnota::instance(); $instance->John->write_message_to('Dude III', 'Got message?'); ?>
由于没有每次都创建单个实例,它们都具有具有随机数的相同公共属性。任何需要重新创建另一个实例的特殊情况也可以通过使用new
关键字轻松完成
<?php print "g::instance()->rand = ".g::instance()->rand."\n"; print "\Gubnota\Gubnota::instance()->rand = ".$instance->rand." equals\n"; print "Creation of new independent pocket of classes:\n"; $instance2 = new \Gubnota\Gubnota(); print "\$instance2->rand = $instance2->rand not equals\n"; print "\Gubnota\Gubnota::instance()->rand = ".$instance->rand." still equals\n"; ?>
外观
为了使用带有完整命名空间的名称而不是在文件中编写,最好使用外观机制进行注册
<?php g::instance() ->empty_facade() // empty all facades default name values ->facade('John','John\Doe') // add default one value included with package ->facade('Smith','John\Doe') // facades can share same class among different names ->delete_facade('Smith') // deletes facade name called 'Smith' ; ?>
注册外观后,您可以在任何地方像这样调用它
<?php g::instance() ->facade('sw_sendmail','\Swift_SendmailTransport') ->facade('sw_smtp','\Swift_SmtpTransport') ->facade('sw_message','\Swift_Message') ->facade('sw_malier','\Swift_Mailer') g::sw_message; ?>
通过composer安装
要能够在您的composer-powered项目中使用,您需要通过composer安装它们。只需将库添加到项目的composer.json
,然后运行php composer.phar install
{ "require": { "php" : ">=5.4", "gubnota/smart_loader": "dev-master" } }
配置和运行时配置
目前,Gubnota\smart_loader或g
符号链接缺少很多它们可能应该接受的配置选项。
故障排除
如果自动加载器不起作用,请确保在composer.json
父项目文件中包含autoload
和config
指令,例如
<?php { "name": "gubnota/your_personal_project", "authors": [ { "name": "Vladislav Muravyev", "email": "me@gubnota.ru" } ], "require": { "php" : ">=5.4", "gubnota/smart_loader": "dev-master" }, "min-stability":"dev-master", "config": { "vendor-dir": "./vendor/" }, "autoload": { "psr-4": {"": ["app/"]} } }
您还可以明确地自动加载文件,例如
"autoload": { "files": ["Gubnota/Gubnota.php","g.php","John/Doe.php"] }