组织对象和库

发布于 2024-12-04 20:42:02 字数 1863 浏览 2 评论 0原文

我一直在 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 技术交流群。

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

发布评论

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

评论(1

向日葵 2024-12-11 20:42:02

将您的库/类文件放在公共目录中。然后,

 www
 |_includes
 | |_classes
 | | |_view.php
 | |_config
 |   |_database.php
 |_other_folder
   |_index.php

您可以在 .htaccess 文件中设置一个公共包含路径到此“includes”目录:

php_value include_path .:/path/to/www/includes

然后 other_folder/index.php 文件只需要类似以下内容:

require_once('config/database.php');
require_once('classes/view.php');

Put your library/class files in a common directory. Something like:

 www
 |_includes
 | |_classes
 | | |_view.php
 | |_config
 |   |_database.php
 |_other_folder
   |_index.php

You can then set a common include path in your .htaccess file to this "includes" directory:

php_value include_path .:/path/to/www/includes

Then the other_folder/index.php file just needs something like:

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