購入する曲を含むカート クラスを作成しました。クラスはcartSong
正常に動作しますが、cart クラスを使用すると、$this
常にエラーが発生します。変数が呼び出される$songList (array)
たびに歌のオブジェクトをカートに追加し、反復する変数が必要です。エラーのある行は、コードで指定されています。addToCart
$trackno
<?php
$indexpath = "index.php";
$cartpath = "data/cart.xml";
class cartSong{
private $albumid = null;
private $trackno = null;
function cartSong($albumid, $trackno){
$this->albumid = $albumid;
$this->trackno = $trackno;
}
function setSong($albumid, $trackno){
$this->albumid = $albumid;
$this->trackno = $trackno;
}
function getTrackNo(){
return $this->trackno;
}
function getAlbumID(){
return $this->albumid;
}
}
class cart{
public $songList;
public $songCount;
function cart(){
$this->songList = array();
$this->songCount = 0;
}
function addToCart($albumid, $trackno){
$checker=0;
for($i=0;$i<$this->songCount;$i++){ // THIS LINE GIVES AN ERROR ($this->songCount)
if( ($this->songList[$i]->getAlbumID()==$albumid) && ($this->songList[$i]->getTrackNo()==$trackno) )
$checker=1;
}
if($checker==0){
$song = new CartSong($albumid, $trackno);
$this->songList[]=$song;
$this->songCount++;
}
else
echo "Song already exists in cart.";
echo $this->songList[0]->getAlbumID();
echo $this->songList[0]->getTrackNo();
}
function removeFromCart($albumid, $trackno){
$checker=0;
for($i=0;$i<count($songList);$i++){
if( ($songList[$i].getAlbumId()==$albumid) && ($songList[$i].getTrackNo()==$trackno) )
$checker=1;
}
if($checker==1){
array_splice($songList,$i);
}
else
echo "Song does not exist in cart.";
}
function emptyCart(){
$songList = (array) null;
}
}
?>
これを実行すると、エラーが 1 つだけ発生します。
致命的なエラー: 40 行目の C:\wamp\www\musiquebasse\data\cartfunctions.php のオブジェクト コンテキストではないときに $this を使用しています。
これが私がコードを呼び出した場所です。これは addtocart.php です。
<?php
$indexpath = "index.php";
require_once "data/SessionControl.php";
require_once "data/cartfunctions.php";
$album = $_GET["albumid"];
$track = $_GET["trackno"];
$action = $_GET["action"];
$cart = new cart();
// insert checker here (if the same song is added to cart
switch($action) { //decide what to do
case "add":
$cart::addToCart($album, $track);
break;
case "remove":
$cart::removeFromCart($album, $track);
break;
case "empty":
$cart::emptyCart();
break;
}
?>