嗨,大家好我开始学习PHP OOP。我有个问题。在下面的例子中,为什么我需要声明var $ price,var $ title;并使用它在 echo $ this-> title = $ par ;.其中,我可以直接回声$面值
<?php
class Books{
/* Member variables */
var $price;
var $title;
/* Member functions */
function setPrice($par){
$this->price = $par;
}
function getPrice(){
echo $this->price ."<br/>";
}
function setTitle($par){
echo $this->title = $par;
//echo $par; what is the difference between the two.
}
function getTitle(){
echo $this->title ." <br/>";
}
}
?>
<?php
$physics = new Books;
$maths = new Books;
$chemistry = new Books;
$physics->setTitle("Physics for High School");
$chemistry->setTitle("Advanced Chemistry");
$maths->setTitle("Algebra");
$physics->setPrice(10);
$chemistry->setPrice(15);
$maths->setPrice(7);
$physics->getTitle();
$chemistry->getTitle();
$maths->getTitle();
$physics->getPrice();
$chemistry->getPrice();
$maths->getPrice();
什么?你现在的问题是什么?为什么你必须写:'$ this-> price = $ par;'或'echo $ this-> price。“
”;''''或'var $ price'? – Rizier123
学习oop,不要使用'var'。你从中学到的东西是非常过时的 –
如果你不做任务,'$ par'不会保存在对象中。 – Barmar