为什么我不能在 PHP 中将实例变量声明为对象数组?
我正在用 PHP 编写一个客户管理系统,供离线使用(即在客户端计算机上)。我考虑过使用 Java 或 C#,但得出的结论是,让浏览器为我完成所有布局更容易,只需让公司在他们的计算机上安装 wamp 即可。
通过此界面,他们还可以管理代理(即在其区域内为公司获取订单的销售人员,以防有人不知道)。这是我将在这篇文章中使用的部分来演示我遇到的问题。
基本上我有 4 个类 - AgentPages、AgentList、AgentDetails 和 AgentForm。 AgentForm 将有两种模式 - 编辑和新建。 AgentPages 有一个名为 getPages 的函数,它返回其他 3 个类的实例数组。然而它不喜欢“new”关键字。
我的代码如下(仅适用于 AgentPages 类):
<?php
require_once("AgentList.php");
require_once("AgentDetails.php");
require_once("AgentForm.php");
class AgentPages {
public function __construct() {
echo "Constructed";
}
private $pages = array("List" => new AgentList(), "Details" => new AgentDetails(), "Form" => new AgentForm());
function getPages() {
return $this->pages;
}
}
?>
我正在使用启用了 PHP 的 netbeans 6.9 IDE,并且(正如您可能猜到的那样)我安装了 wamp 服务器。在 PHP 版本 5.3 下,netbeans 调试器告诉我“解析错误:第 20 行 C:\wamp\www\CustomerApp_v2\Agents\AgentPages.php 中的解析错误”。在 5.2.11 下,它在该行上说了一些关于意外 T_NEW 的内容。我已经在第 20 行之前删掉了一大段注释,但我可以告诉你,第 20 行是 $pages 的声明。目前我每个类都有一个空的构造函数。
我还尝试了以下行而不是第 20 行:
$AgentList = new AgentList();
这也不起作用 - 我收到相同的错误。根据我看过的所有教程,我的代码没有任何问题 - 但我可能只是忽略了一些明显的东西。
有谁知道我做错了什么?我之前做过很多PHP面向对象的东西,但我最后一次接触它已经是两年前了。
提前致谢。
问候,
理查德
I am writing a customer management system in PHP, for use offline (i.e. on the clients computer). I have considered using Java or C# but have come to the conclusion that it is easier to let the browser do all the layout for me, and just have the company install wamp on their computers.
Through this interface they will also be able to manage Agents (i.e. salesmen that go round their area getting orders for the company, in case any doesn't know). This is the section I will use in this post to demonstrate the problem I am having.
Basically I have 4 classes - AgentPages, AgentList, AgentDetails and AgentForm. AgentForm will have two modes - edit and new. AgentPages has a function called getPages, which returns an array of instances of the other 3 classes. However it does not like the "new" keyword.
My code is as follows (for the AgentPages class only):
<?php
require_once("AgentList.php");
require_once("AgentDetails.php");
require_once("AgentForm.php");
class AgentPages {
public function __construct() {
echo "Constructed";
}
private $pages = array("List" => new AgentList(), "Details" => new AgentDetails(), "Form" => new AgentForm());
function getPages() {
return $this->pages;
}
}
?>
I am using the netbeans 6.9 IDE with PHP enabled, and (as you can probably guess) I have wamp server installed. Under PHP version 5.3 the netbeans debugger is telling me that "Parse error: parse error in C:\wamp\www\CustomerApp_v2\Agents\AgentPages.php on line 20". Under 5.2.11 it says something about unexpected T_NEW on that line. I have cut out a large comment on this, before line 20, but I can tell you that line 20 is the declaration of $pages. I have an empty constructor for each class at the moment.
I have also tried the following line instead of line 20:
$AgentList = new AgentList();
This doesnt work either - I get the same error. According to all the tutorials I have looked at there is nothing wrong with my code - I am probably just overlooking something obvious though.
Does anyone have any idea what I am doing wrong? I have done lots of PHP object oriented stuff before now, but the last time I touched it was 2 years ago.
Thanks in advance.
Regards,
Richard
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是您尝试使用表达式初始化声明中的实例变量(对
new
的调用是一个表达式)。那是行不通的。将赋值放入构造函数中,它就会起作用。像这样:
The problem is that you're trying to initialize an instance variable in a declaration with an expression (a call to
new
is an expression). That doesn't work. Put the assignment in the constructor and it will work.Like this: