php5下类内部变量引用错误?请各位帮忙解决!

林仪明 2004-10-27 09:24:23
php5 + iis5

class Temp{
private $action;
private $html;

public function _construct(){
}

function getAction(){
return $this->$servlet;
}
function getHtml(){
return $this->$html;
}

function setAction($_action){
$this->$action = $_action;
}
function setHtml($_html){
$this->$html = $_html;
}
}

class Config{

function getName($name){
$Temp = new Temp();
$action = new Action();
$html = 2;
$screen->setServlet($action);
$screen->setTemplate($html);
return $temp;
}
}
class A{
function execute(){
$config = new Config();
$aa = "random";
$tt = $config->getName($aa);
print($tt->getAction());
}
}

页面显示:
2

明显是对象引用内存地址不对,这个是为什么啊?
...全文
235 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
林仪明 2004-11-05
  • 打赏
  • 举报
回复
There is the Cart class
class Cart {
var $todays_date;
var $name;
var $owner;
var $items = array("VCR", "TV");

function Cart() {
$this->todays_date = date("Y-m-d");
$this->name = $GLOBALS['firstname'];
/* etc. . . */
}
}
林仪明 2004-11-05
  • 打赏
  • 举报
回复
There is the answer:
The variable is named $cart->items, not $cart->$items, that is, a variable name in PHP has only a single dollar sign.

<?php
// correct, single $
$cart->items = array("10" => 1);

// invalid, because $cart->$items becomes $cart->""
$cart->$items = array("10" => 1);

// correct, but may or may not be what was intended:
// $cart->$myvar becomes $cart->items
$myvar = 'items';
$cart->$myvar = array("10" => 1);
?>
surfchen 2004-11-04
  • 打赏
  • 举报
回复
哦~原来是这个错~~

其实我也经常多加或少加$的~~我在这里也遇到过几次有人问问题是因为这个了~~

看来自己还不够细心呀`~~~惭愧惭愧
林仪明 2004-11-04
  • 打赏
  • 举报
回复
okay, I had solve it myself. It's due to my fault that I write "$this->$html" to access class members, but "$this->html" is correct.
Thanks all guys. Thank you for your paying attention to this.

to surfchen(冲浪) :
OOD is a solutions and methods set to help we to build a system. Every languages has its spec to implements those. In this issue, it's a solecism that I made. In the case of I write a new class to replace it, It will still not work. But thanks you all same.

hahawen 2004-10-31
  • 打赏
  • 举报
回复
to: lins(Anders*小明)

贴你的全部代码好吗?你给几个类有什么用啊?你是怎么调用的?你的测试代码呢?
surfchen 2004-10-31
  • 打赏
  • 举报
回复
www.phpe.net

多去看一些PHP5的关于OO的文章!你写的这个类,不要也罢
林仪明 2004-10-31
  • 打赏
  • 举报
回复
没有人可以帮忙吗?
surfchen 2004-10-28
  • 打赏
  • 举报
回复
function _construct()你这个还是没有改~~~
林仪明 2004-10-28
  • 打赏
  • 举报
回复
对不起,昨天代码没有贴好:
class Action{
function doAction(){
print("test");
}
}
class Temp{
private $action;
private $html;

public function _construct(){
}

function getAction(){
return $this->$action;
}
function getHtml(){
return $this->$html;
}

function setAction($_action){
$this->$action = $_action;
}
function setHtml($_html){
$this->$html = $_html;
}
}

class Config{

function getName($name){
$Temp = new Temp();
$action = new Action();
$html = 2;
$screen->setAction($action);
$screen->setTemplate($html);
return $temp;
}
}
class A{
function execute(){
$config = new Config();
$aa = "random";
$tt = $config->getName($aa);
print($tt->getAction());
}
}
netstu 2004-10-28
  • 打赏
  • 举报
回复
毛病多多:)
林仪明 2004-10-28
  • 打赏
  • 举报
回复
真是不好意思!由于一些缘故,贴出来的代码是修改的,删了很多。所以一直有问题。给各位好心人带来麻烦!
林仪明 2004-10-28
  • 打赏
  • 举报
回复
晕啊!
class Config{

function getName($name){
$Temp = new Temp();
$action = new Action();
$html = 2;
$Temp->setAction($action);
$Temp->setHtml($html);
return $temp;
}
}
xuzuning 2004-10-28
  • 打赏
  • 举报
回复
Fatal error: Call to undefined method Temp::setTemplate()
林仪明 2004-10-28
  • 打赏
  • 举报
回复
对不起,代码还是没有给好,config代码如下
class Config{

function getName($name){
$Temp = new Temp();
$action = new Action();
$html = 2;
$Temp->setAction($action);
$Temp->setTemplate($html);
return $temp;
}
}
xuzuning 2004-10-28
  • 打赏
  • 举报
回复
代码不全!
$screen在哪里实例化的?
林仪明 2004-10-28
  • 打赏
  • 举报
回复
我现在没有用到$name这个参数,因为我程序没有写完,我现在只是在做测试。但就可运行的程序来看,temp类中的两个内部成员变量指向了同一个内存地址,而在其他代码中没有出现这样的情况。我很奇怪。希望能得到帮助
surfchen 2004-10-28
  • 打赏
  • 举报
回复
function getName($name)???这个$name参数有什么用?在你的类的方法里这个参数根本没用到~~莫名其妙
林仪明 2004-10-28
  • 打赏
  • 举报
回复
function _construct()已经改过来了
function __construct()
surfchen 2004-10-27
  • 打赏
  • 举报
回复
还有~~你的Action类呢?
surfchen 2004-10-27
  • 打赏
  • 举报
回复
扫了一眼``~~发现你这个写错了public function _construct(){
是__construct,两横的~~
加载更多回复(1)

21,887

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧