[PHP]在不同的文件中使用类的对象 [英] [PHP] using object of a class in different files

查看:149
本文介绍了[PHP]在不同的文件中使用类的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我有一个php文件说class.php其中定义了一个数据库类。

我有另一个文件,即config.php;将其对象初始化为

hi guys,

I have a php file say "class.php" in which a database class is defined.
I have got another file namely "config.php" which initializes its object as

展开 | 选择 | Wrap | 行号

推荐答案




我最近有类似的问题。没有全局的我解决了这个问题。解决方案是创建数据库对象的实例,然后在创建另一个类的实例时将数据库对象传递给它并将其设置为第二个类的属性。


如果你只是想在普通的php页面上使用它,然后我也做到了这一点。我的所有网站页面都包含pageheader.php。此文件实例化我的数据库类。由于这包含在所有页面上,因此数据库对象在所有页面上都可用。


我希望这是有道理的。


干杯

nathj
Hi,

I had a similar issue myself recently. I got around this without globals. The solution was to create an instance of the database object, then when creating an instance of another class pass the database object into it and set it as a property of the second class.

If you are simply wanting to use it on normal php pages, then I ahve also done this. All my site pages include pageheader.php. This file instantiates my database class. As this is included on all the pages the database object is available on all the pages.

I hope that makes sense.

Cheers
nathj


我觉得你想说...我的坏,但你可以解释一下以示例,请求


谢谢:)
i din get waht u want to say .. my bad, but can u explain it with example, plese

thanks :)



i din get waht你想说..我的坏,但你可以用例子解释一下,请求


谢谢:)
i din get waht u want to say .. my bad, but can u explain it with example, plese

thanks :)



你好,


无需道歉 - 我的解释充其量是模糊的。这是一个类中的类的示例:


主要php页面(不是类定义)

[php]

<?php

require_once(

Hi,

No need for an apology - my explanation was at best fuzzy. Here''s an example of a class within a class:

Main php page (not class definition)
[php]
<?php
require_once(


这篇关于[PHP]在不同的文件中使用类的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆