作者b8888 (b8888)
看板PHP
標題[請益] OO的問題?
時間Mon Aug 18 16:12:08 2008
各位大大好~~~
小弟在練習PHP程式時,有關OO部份~~
<?php
class cart {
var $owner ;
var $price =300 ;
var $goods ;
function add_item($title,$amt)
{
$this->goods[$title]+=$amt;//this is line 9
return $amt;
}
function remove_item($title,$amt){
$this->goods[$title]-=$amt;
return $amt;
}
function get_owner(){
return $this->owner;
}
function get_goods(){
return $this->goods;
}
}
$item=0;
$mycart =new cart ;
$mycart->owner ="Linda";
$item+=$mycart->add_item("Word",3);
$item+=$mycart->add_item("Word",1);
$item+=$mycart->add_item("Access",2);
$list = $mycart->get_goods();
print_r($list) ;
===============================================
但是輸出時~~~
Notice: Undefined index: Word in /var/www/html/callcenter/aa12.php on line 9
Notice: Undefined index: Access in /var/www/html/callcenter/aa12.php on line 9
6Array ( [Word] => 4 [Access] => 2 )
輸出的結果是對的~~
如wore 輸出是4;Access輸出是2
請問各位大大為何會出現二行錯誤訊息~~~
-->小弟推測是開始時index 没有Word 或 Access 當索引值~~~
不知該如何改寫~~
才不會出現錯誤訊息
謝謝您的幫忙~~
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 163.29.241.8
1F:推 clifflu :可以用 isset 做檢查,然後用 @ 把該行的警告壓掉 08/18 16:26
2F:→ clifflu :<-- 一陣子沒寫 php 了 希望沒記錯 08/18 16:26
3F:→ tsangbor :第一段先加上error_reporting( E_ALL ^ E_NOTICE ); 08/18 22:18
4F:→ marcoyan :$this->goods[$title]應該為陣列,用法應該是 08/19 10:23
5F:→ marcoyan :自爆一下,我試沒有錯誤 = =" 只是覺得用法怪怪 08/19 10:27