PHP中的动态类名
我正在尝试创建一个系统,它有一个GeneralObj。GeneralObj允许用户使用表名字符串为数据库的表启动特殊对象。到目前为止,它工作得很好PHP中的动态类名,php,dynamic,autoload,Php,Dynamic,Autoload,我正在尝试创建一个系统,它有一个GeneralObj。GeneralObj允许用户使用表名字符串为数据库的表启动特殊对象。到目前为止,它工作得很好 class GeneralObj{ function __construct($tableName) { //... } } 但是,每次输入新的GeneralObj(XXX)太累了。 我想知道是否有可能简化流程,使之像new XXX(),它实际上与new GeneralObj(XXX)一样运行? 我在设置includ
class GeneralObj{
function __construct($tableName) {
//...
}
}
但是,每次输入新的GeneralObj(XXX)太累了。
我想知道是否有可能简化流程,使之像new XXX(),它实际上与new GeneralObj(XXX)一样运行?
我在设置include_path中发现PHP为动态加载文件提供的_自动加载方法,但它需要一个实际存在的定义文件。我真的不想复制和复制相同的定义文件,只是稍微改变一下
因为,评估不是一个选项。不,这是不可能的 该扩展允许对PHP运行时环境进行编程操作,但无法做到这一点。即使可以,这也将是一个非常糟糕的主意,它将极大地影响应用程序的需求和复杂性,以换取节省一些击键
在一个不相关的注释中,您的
GeneralObj
类的功能听起来很像一个。也许你应该考虑用一个[/p>< p>不替换它,这是不可能的。
该扩展允许对PHP运行时环境进行编程操作,但无法做到这一点。即使可以,这也将是一个非常糟糕的主意,它将极大地影响应用程序的需求和复杂性,以换取节省一些击键
在一个不相关的注释中,您的
GeneralObj
类的功能听起来很像一个。也许你应该考虑用一个< /P> > P >这样的自动装订机:
myAutoloader::Register();
class myAutoloader
{
/**
* Register the Autoloader with SPL
*
*/
public static function Register() {
if (function_exists('__autoload')) {
// Register any existing autoloader function with SPL, so we don't get any clashes
spl_autoload_register('__autoload');
}
// Register ourselves with SPL
return spl_autoload_register(array('myAutoloader', 'Load'));
} // function Register()
/**
* Autoload a class identified by name
*
* @param string $pClassName Name of the object to load
*/
public static function Load($pClassName){
if (class_exists($pClassName,FALSE)) {
// Already loaded
return FALSE;
}
$pClassFilePath = str_replace('_',DIRECTORY_SEPARATOR,$pClassName) . '.php';
if (file_exists($pClassFilePath) === FALSE) {
// Not a class file
return new GeneralObj($pClassName);
}
require($pClassFilePath);
} // function Load()
}
function __autoload($class_name) {
// check for classes ending with 'Table'
if (preg_match('/(.*?)Table/', $class_name, $match)) {
$classPath = PATH_TO_TABLES . '/' . $match[1] . '.php';
// auto-create the file
if (!file_exists($classPath)) {
$classContent = "
class $class_name extends GeneralObj {
public __construct() {
parent::__construct('{$match[1]}');
}
}";
file_put_contents($classPath, $classContent);
}
require_once $classPath;
}
}
如果无法实例化table类,则由GeneralObj引发异常,如以下自动加载程序:
myAutoloader::Register();
class myAutoloader
{
/**
* Register the Autoloader with SPL
*
*/
public static function Register() {
if (function_exists('__autoload')) {
// Register any existing autoloader function with SPL, so we don't get any clashes
spl_autoload_register('__autoload');
}
// Register ourselves with SPL
return spl_autoload_register(array('myAutoloader', 'Load'));
} // function Register()
/**
* Autoload a class identified by name
*
* @param string $pClassName Name of the object to load
*/
public static function Load($pClassName){
if (class_exists($pClassName,FALSE)) {
// Already loaded
return FALSE;
}
$pClassFilePath = str_replace('_',DIRECTORY_SEPARATOR,$pClassName) . '.php';
if (file_exists($pClassFilePath) === FALSE) {
// Not a class file
return new GeneralObj($pClassName);
}
require($pClassFilePath);
} // function Load()
}
function __autoload($class_name) {
// check for classes ending with 'Table'
if (preg_match('/(.*?)Table/', $class_name, $match)) {
$classPath = PATH_TO_TABLES . '/' . $match[1] . '.php';
// auto-create the file
if (!file_exists($classPath)) {
$classContent = "
class $class_name extends GeneralObj {
public __construct() {
parent::__construct('{$match[1]}');
}
}";
file_put_contents($classPath, $classContent);
}
require_once $classPath;
}
}
如果无法实例化table类,则由GeneralObj引发异常也许您可以在autoloader中自动创建文件:
myAutoloader::Register();
class myAutoloader
{
/**
* Register the Autoloader with SPL
*
*/
public static function Register() {
if (function_exists('__autoload')) {
// Register any existing autoloader function with SPL, so we don't get any clashes
spl_autoload_register('__autoload');
}
// Register ourselves with SPL
return spl_autoload_register(array('myAutoloader', 'Load'));
} // function Register()
/**
* Autoload a class identified by name
*
* @param string $pClassName Name of the object to load
*/
public static function Load($pClassName){
if (class_exists($pClassName,FALSE)) {
// Already loaded
return FALSE;
}
$pClassFilePath = str_replace('_',DIRECTORY_SEPARATOR,$pClassName) . '.php';
if (file_exists($pClassFilePath) === FALSE) {
// Not a class file
return new GeneralObj($pClassName);
}
require($pClassFilePath);
} // function Load()
}
function __autoload($class_name) {
// check for classes ending with 'Table'
if (preg_match('/(.*?)Table/', $class_name, $match)) {
$classPath = PATH_TO_TABLES . '/' . $match[1] . '.php';
// auto-create the file
if (!file_exists($classPath)) {
$classContent = "
class $class_name extends GeneralObj {
public __construct() {
parent::__construct('{$match[1]}');
}
}";
file_put_contents($classPath, $classContent);
}
require_once $classPath;
}
}
也许您可以在自动加载器中自动创建文件:
myAutoloader::Register();
class myAutoloader
{
/**
* Register the Autoloader with SPL
*
*/
public static function Register() {
if (function_exists('__autoload')) {
// Register any existing autoloader function with SPL, so we don't get any clashes
spl_autoload_register('__autoload');
}
// Register ourselves with SPL
return spl_autoload_register(array('myAutoloader', 'Load'));
} // function Register()
/**
* Autoload a class identified by name
*
* @param string $pClassName Name of the object to load
*/
public static function Load($pClassName){
if (class_exists($pClassName,FALSE)) {
// Already loaded
return FALSE;
}
$pClassFilePath = str_replace('_',DIRECTORY_SEPARATOR,$pClassName) . '.php';
if (file_exists($pClassFilePath) === FALSE) {
// Not a class file
return new GeneralObj($pClassName);
}
require($pClassFilePath);
} // function Load()
}
function __autoload($class_name) {
// check for classes ending with 'Table'
if (preg_match('/(.*?)Table/', $class_name, $match)) {
$classPath = PATH_TO_TABLES . '/' . $match[1] . '.php';
// auto-create the file
if (!file_exists($classPath)) {
$classContent = "
class $class_name extends GeneralObj {
public __construct() {
parent::__construct('{$match[1]}');
}
}";
file_put_contents($classPath, $classContent);
}
require_once $classPath;
}
}
使用继承。使GeneralObj成为表特定类的超类。通过这种方式,您可以动态派生类名并实例化对象。例如:
class someTable extends GeneralObj {
}
$tableName = 'some';
$className = $tableName . 'Table';
$obj = new $className;
使用继承。使GeneralObj成为表特定类的超类。通过这种方式,您可以动态派生类名并实例化对象。例如:
class someTable extends GeneralObj {
}
$tableName = 'some';
$className = $tableName . 'Table';
$obj = new $className;
修改你的自动加载器-你正在使用自动加载器,不是吗?你这样做后的计划是什么。因为我认为这样做不是一个好主意。缩短名称。。。如果你太累了…修改你的自动加载器-你正在使用自动加载器,不是吗?在你这样做之后,计划是什么。因为我认为这样做不是一个好主意。缩短名称。。。如果您太累了…是否可以从自动加载处理程序返回实例?我以前从未见过,医生也没有提到这种可能性。不确定,我从未实际测试过。。。虽然无效
表名的异常处理一般来说是一件非常痛苦的事情,但我还是要试一试。谢谢你,我从来没有听说过spl_autoload_register()。但在对这组api进行了一些研究之后,我仍然无法使其正常工作。我和@Jon有同样的问题,似乎自动加载返回实例并不例外。这是一个测试程序,它会触发一个错误,使程序无法按预期完成。如果可能的话,请分享你使用的PHP版本,这可能会有很大帮助。我的版本是5.2.6,不是最新版本,有些功能可能无法使用。是否可以从自动加载处理程序返回实例?我以前从未见过,医生也没有提到这种可能性。不确定,我从未实际测试过。。。虽然无效表名的异常处理一般来说是一件非常痛苦的事情,但我还是要试一试。谢谢你,我从来没有听说过spl_autoload_register()。但在对这组api进行了一些研究之后,我仍然无法使其正常工作。我和@Jon有同样的问题,似乎自动加载返回实例并不例外。这是一个测试程序,它会触发一个错误,使程序无法按预期完成。如果可能的话,请分享你使用的PHP版本,这可能会有很大帮助。我的版本是5.2.6,不是最新版本,有些功能可能不起作用。问题是,我甚至不想使用继承。类定义很烦人,没有意义。仅更改表的名称不必重复定义。在你的例子中,我们必须定义尽可能多的类和数据库中的表。我明白你的意思,但是OO是一种折衷。。。为了可读性和可维护性,您需要编写更多的代码/创建更多的类。模块化设计/可插拔组件等。所有这些都最好使用继承和其他模式实现。在我看来,OO不应该是黄金法则。OO之所以如此重要,是因为它将设计层和程序层分离,并将系统结构化。这些特性使生成的代码可读且易于管理。然而,在OO发展了50多年的今天,OO已经偏离了它的目标,应用OO的系统变得过于结构化。即使是一个小的系统也应该被分成几十个文件、类。这是可怕的和可跳过的。就像我正在工作的系统一样,它只是一个ORM系统,应该可以轻松地移植到其他数据库,这意味着它没有理由几乎不定义任何表对象类。由于这些原因,Hibernate和EJB是失败的产品,因为它几乎不可移植,而且很难使用,即使我们有遗传工具来处理繁重的任务。世界需要动态的。您可能会在编程语言中看到许多超新星,如Ruby、Python和Lisp等都是动态的。他们没有理由不能通过周密的计划来构建一个大的、有用的系统。问题是,我甚至不想使用继承。类的定义很烦人,没有意义