我是一个新手试图设计一个计算学生分数的应用程序。我正试图用OOP简化我的工作,我在这里一直有错误。这是我上课:
class fun {
var $totalscore;
public function score($assignment,$cat,$exam){
return $totalscore = $assignment+$cat+$exam;
if($totalscore <=100 && $totalscore >=70){
return $grade = "A";
}
elseif($totalscore <=69 && $totalscore>=60){
return $grade = "B";
}
elseif($totalscore <=59 && $totalscore>=50){
return $grade = "C";
}
elseif($totalscore <=35 && $totalscore>=49){
return $grade = "D";
}
elseif($totalscore <=40 && $totalscore>=34){
return $grade = "E";
}
elseif($totalscore <=39 && $totalscore>=0){
return $grade = "F";
}
}
}
现在我试图调用变量我的意思是$ totalscore和$ grade在我的其他php下面
if(isset($_POST['update'])){
$gnsa = $_POST['gnsa'];
$gnst =$_POST['gnst'];
$gnse =$_POST['gnse'];
$agidi =$_POST['matric'];
include ("class.php");
$fun = new fun;
$fun-> score($gnsa,$gnst,$gnse);
if($totalscore > 100){
echo "invalid score";
}
}
答案 0 :(得分:0)
使用你的课时你正确地调用了这样的方法:
$fun->score($gnsa,$gnst,$gnse);
类的变量(通常称为成员或属性)只是被类似地调用(前提是它们是公共的):
if($fun->totalscore > 100){
echo "invalid score";
}
答案 1 :(得分:0)
class fun
{
// notice these 2 variables... they will be available to you after you
// have created an instance of the class (with $fun = new fun())
public $totalscore;
public $grade;
public function score($assignment, $cat, $exam)
{
$this->totalscore = $assignment + $cat + $exam;
if ($this->totalscore >= 70) {
$this->grade = "A";
}
else if ($this->totalscore <= 69 && $this->totalscore >= 60) {
$this->grade = "B";
}
else if ($this->totalscore <= 59 && $this->totalscore >= 50) {
$this->grade = "C";
}
else if ($this->totalscore <= 35 && $this->totalscore >= 49) {
$this->grade = "D";
}
// there is probably something wrong here... this number (40) shouldn't
// be higher than the last one (35)
else if ($this->totalscore <= 40 && $this->totalscore >= 34) {
$this->grade = "E";
}
else {
$this->grade = "F";
}
}
}
现在,在您$fun->score($gnsa,$gnst,$gnse);
之后,您将能够分别使用$fun->totalscore
和$fun->grade
访问总分和成绩。