组织对象和库
我一直在 php 中从头开始创建少量的库/类。我来自 codeigniter 背景,我正在尝试制作一些具有类似功能的库。我不断遇到有关对象的问题。
以某种方式创建超级对象 $this
的最佳方法是?我的主要问题是我创建了一个视图对象,并运行了一个名为 load
的函数,如下所示:
class View {
public function __construct() {
}
public function load($file = NULL, $data = array()) {
if($file) {
$file .= '.php';
if(file_exists($file)) {
// Extract variables BEFORE including the file
extract($data);
include $file;
return TRUE;
} else {
echo 'View not found';
return FALSE;
}
} else {
return FALSE;
}
}
}
然后在我的 php 文件中,我在顶部 include 'libraries.php ';
看起来像:
include 'database.php';
include 'view.php';
include 'input.php';
include 'form.php';
$config = array(
'host' => 'localhost',
'username' => 'username',
'password' => 'password',
'database' => 'database'
);
$database = new Database($config);
$view = new View();
$input = new Input();
$form = new Form();
从我包含库的文件中,我可以毫无错误地编写类似 $form->value('name');
的内容。但是,如果我执行以下操作:
$view->load('folder/index', array('var_name' => 'var_value'));
然后从 folder /index.php
文件我可以很好地访问 $var_name
,但不能访问 $form->value('name');
。我收到诸如 Call to a member function value() on a non-object in ...
之类的错误。
我的问题是如何以可重用的方式组织我的库和类。我不想使用前端加载程序(所有内容都首先运行的 index.php
文件)。这可能是我编写课程的方式的问题,但我想这是一个更大的问题,涉及事物的位置等。
I've been creating a small number of libraries / classes from scratch in php. I come from a codeigniter background, and I'm trying to make some libraries with similar functionality. I keep running into issues regarding objects.
Is the best way to create a super object $this
somehow? My main issue is that I've created a view object and I run a function called load
which looks like so:
class View {
public function __construct() {
}
public function load($file = NULL, $data = array()) {
if($file) {
$file .= '.php';
if(file_exists($file)) {
// Extract variables BEFORE including the file
extract($data);
include $file;
return TRUE;
} else {
echo 'View not found';
return FALSE;
}
} else {
return FALSE;
}
}
}
Then in my php file, I have at the top include 'libraries.php';
which looks like:
include 'database.php';
include 'view.php';
include 'input.php';
include 'form.php';
$config = array(
'host' => 'localhost',
'username' => 'username',
'password' => 'password',
'database' => 'database'
);
$database = new Database($config);
$view = new View();
$input = new Input();
$form = new Form();
From the file which I included the libraries, I am able to write something like $form->value('name');
without errors. However, if I do something like this:
$view->load('folder/index', array('var_name' => 'var_value'));
then from the folder/index.php
file I can access $var_name
just fine, but not $form->value('name');
. I get errors such as Call to a member function value() on a non-object in ...
My question is how can I organize my libraries and classes in a way that will be reusable. I don't want to use a front loader (an index.php
file that everything runs through first). This may be an issue with the way I wrote my classes, but I imagine it's a larger issue regarding where things are located etc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将您的库/类文件放在公共目录中。然后,
您可以在 .htaccess 文件中设置一个公共包含路径到此“includes”目录:
然后 other_folder/index.php 文件只需要类似以下内容:
Put your library/class files in a common directory. Something like:
You can then set a common include path in your .htaccess file to this "includes" directory:
Then the other_folder/index.php file just needs something like: