- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
这是我的第一个问题,也是让我感到难过的问题。我不确定这是简单的事情,我忽略了它还是不可能的事情。
下面是我的原始代码的一个非常简化的版本。最终目标是输出如下:
1:
2: this is a test
3: this is another test
4: this is another test
但是,对于当前状态的代码,其实际输出是这样的:
1:
2: this is a test
3: this is another test
4:
我希望对象“B”能够在 first_function() 更改它之后访问 test_variable 的值。
当我将 test_variable 声明为静态时,它工作正常,但在实际应用程序中它不起作用,当我尝试回显 parent::test_variable 时,它输出“Object ID #17”等等。
class A
{
public $test_variable;
function __construct()
{
echo '1: ' . $this->test_variable . "<br />";
$this->test_variable = 'this is a test';
echo '2: ' . $this->test_variable . "<br />";
}
function first_function()
{
$this->test_variable = 'This is another test';
echo '3: ' . $this->test_variable . "<br />";
$b = new b;
$b->second_function();
}
}
class B extends A
{
function __construct()
{
/* Dont call parent construct */
}
function second_function()
{
echo '4: ' . $this->test_variable;
}
}
$a = new A;
$a->first_function();
// Outputs:
// 1:
// 2: this is a test
// 3: this is another test
// 4:
// but I want it to output
// 1:
// 2: this is a test
// 3: this is another test
// 4: this is another test
非常感谢您的回复。我非常感谢他们。
菲尔
最佳答案
在类中声明 public $test_variable;
意味着类的每个实例(对象)都有一个副本。 A 类中的 $test_variable
与 B 类中的 $test_variable
指向的内存地址不同。这样做是为了允许作用域并删除全局状态。正如您之前所说,将其声明为 static 会起作用,因为这样每个实例都会共享相同的变量。
在这种情况下,$test_variable
本质上是 B 类所需的依赖项。您可以很容易地通过构造函数注入(inject)获得该依赖项:
class A
{
public $test_variable;
function __construct()
{
echo '1: ' . $this->test_variable . "<br />";
$this->test_variable = 'this is a test';
echo '2: ' . $this->test_variable . "<br />";
}
function first_function()
{
$this->test_variable = 'This is another test';
echo '3: ' . $this->test_variable . "<br />";
// Instantiate instance passing dependency
$b = new b($this->test_variable);
$b->second_function();
}
}
class B extends A
{
function __construct($dependency)
{
// Set dependency
$this->test_variable = $dependency;
}
function second_function()
{
echo '4: ' . $this->test_variable;
}
}
$a = new A;
$a->first_function();
所以,这只是关于您可能会考虑如何处理它的想法。
关于PHP5面向对象: Accessing changed parent properties,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11763570/
我正在尝试编写 Access Access 数据库的脚本,以便在命令行上使用。 Access 数据库使用工作组文件进行保护。 Dim oApp, sWGF,myWS Set sApp = Create
我有一个包含数据表的表格。我希望用户能够选择多行,单击按钮并运行一些 sql 查询并对这些行执行一些工作。 查看我的 VBA 代码,我发现如何使用 CurrentRecord 属性 Access 最后
如果我在某个网络位置有 Microsoft Access 2007 数据库,那么可以使用该数据库的客户端计算机的数量是否有限制?客户端不会安装 Access,而是使用 Access Runtime 2
我正在开发一个注册系统。但我收到此错误:You tried to execute a query that does not include the specified expression.. 我正
我有一个产品设计为使用 MS Access 文件作为数据库的桌面产品。 现在,一些用户需要将它安装在几台 PC(比如说 2 或 3 台)上并共享数据库。 我想将 MS Access 文件放在共享文件夹
我接手了一个旧的软件项目,该项目使用 MS Access 数据库来存储数据。但是数据库不会在 Access 中打开,如下所示: "You do not have the necessary permi
我有一个文件夹,里面装满了 100 多个 Access97 文件。我需要将它们全部更新到 Access2003。 我可以手动完成,但使用 VBA 可能会快很多。 有没有人会有一个片段可以做到这一点?或
我正在通过 SQL Server 迁移助手 (SSMA) 将数据从 Access 数据库迁移到 SQL Server。 Access 应用程序将继续与转换为链接表的本地表一起使用。 一个连续的表单在加
我正在通过 SQL Server 迁移助手 (SSMA) 将数据从 Access 数据库迁移到 SQL Server。 Access 应用程序将继续与转换为链接表的本地表一起使用。 一个连续的表单在加
我的公司用 Visual Basic 6 开发了一个应用程序。 该应用程序通过 ODBC 数据源使用 Access 数据库。 Access 数据库是一个扩展名为“.mdb”的文件。 在以下环境中运行应
我一直在尝试让 Microsoft Access 从主 Access 窗口中“退出”,以便我可以隐藏 Access 窗口并仅在桌面上显示表单,以便可以轻松地将其放置在其他应用程序旁边。 起初我发现了一
我想在 access 2010 中使用 access 2000 和 2003 数据库。由于我不想检查一切是否手动工作,我正在寻找一种工具来分析 VBA 代码以查找使用 access 2010 发生的错
所以我有一个 Excel 工作簿,其中有一个很好的 shaperange 对象的全局 map 。通过一些非常简单的代码,我可以更改颜色、将国家/地区集合分组和取消分组为数组等......并且效果非常好
我们希望有大约 35-40 人通过共享驱动器上的脚本写入 Access 数据库。这些指标分解为他们需要每小时写大约 3-7 次。 Access 会支持这一点而不会对我产生影响吗? 是的,我很乐意将其用
我正在寻找一种使用 VBA 代码从外部数据库文件中删除 VBA 模块的方法。名为“myfile.accdb”的外部文件有一个名为“mod1”的模块,我希望能够在单独的项目中使用 VBA 代码删除该模块
我在 Access 2003 数据库(在 Access 2007 中开发)中有三个表单,它们处于父级 -> 子级 -> 孙子级关系中。在子窗体的 'Form_Load' 子窗体中,我设置了孙子窗体的一
MS Access 2007 存在拒绝在设计模式下显示表单的问题。我可以看到表单的代码(如果我查看显示表单的按钮的事件属性),但我看不到作为 GUI 布局的表单。而且,当我尝试从应用程序的主窗口调用此
我编写了代码,使用 Excel 中的下拉列表提供的标准将两个表连接起来,然后将数据返回到电子表格上的特定位置(工作表上已经有标题)。 这在我的机器上和其他机器上使用 MS Access 的机器上都可以
我正在开始构建一个应用程序,该应用程序从给定的根路径开始遍历文件夹结构,并将所有找到的 Access 1997 .mdb 文件转换为较新的 Access 2007/2010 .accdb 格式。但是,
我有一个表单和一个按钮。我想通过单击按钮打开另一个表单,并将参数从父表单传递到子表单(子表单的 RecordSource 有参数)。我该怎么做? 最佳答案 您可以通过引用表单的对象来引用调用表单的任何
我是一名优秀的程序员,十分优秀!