[請益] OO的問題?
各位大大好~~~
小弟在練習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
推
08/18 16:26, , 1F
08/18 16:26, 1F
→
08/18 16:26, , 2F
08/18 16:26, 2F
→
08/18 22:18, , 3F
08/18 22:18, 3F
→
08/19 10:23, , 4F
08/19 10:23, 4F
→
08/19 10:27, , 5F
08/19 10:27, 5F
PHP 近期熱門文章
PTT數位生活區 即時熱門文章