只返回一个元素,OO PHP

时间:2021-09-06 22:19:27
class Score
{
    var $score;
    var $name;
    var $dept;
    var $date;

    function Score($score, $name, $dept, $date)
    {
        $this->scores = ($score);
        $this->name = ($name);
        $this->dept = ($dept);
        $this->date = ($date);
    }

    function return_score(){
        return $this->scores;
        return $this->name;
        return $this->dept;
        return $this->date;
    }
}

$newscore = new Score("131313","James", "Marketing", "19/05/2008");
echo $newscore->return_score();

The above code is only echoing 131313. I am just beginning to learn OO PHP so please go easy! Totally lost, so any help would be much appreciated.

上面的代码只是回应131313.我刚刚开始学习OO PHP,所以请放心!完全输了,所以任何帮助都会非常感激。

3 个解决方案

#1


2  

You can't return more than once in a function. You could return a concatenated string:

您不能在函数中返回多次。您可以返回连接的字符串:

return $this->scores.' '.this->name.' '.$this->dept.' '.$this->date;
//added spaces for readability, but this is a silly thing to do anyway...

I wouldn't recommend it though as you'd be mixing you presentation of the object with its functionality - don't.

我不推荐它,因为你要将对象的呈现与其功能混合 - 不要。

I'd suggest making a template of some sort (I'm imagining you might want to tabulate this data?). Each row would look something like:

我建议制作某种模板(我想你可能想把这些数据制成表格?)。每行看起来像:

<tr>
  <td><?php echo $score->name; ?></td>
  <td><?php echo $score->scores; ?></td>
  <!-- more cells for more properies? -->
</tr>

and giving it your object or objects in array (you know about foreach{} ?). I know it looks more long-winded, but separating these concerns is going to be better for you in the long run.

并给它你的对象或数组(你知道foreach {}?)。我知道它看起来更加冗长,但从长远来看,将这些问题分开对你来说会更好。

Assigning with = : you don't need the parentheses around the thing being assigned (usually).

使用=分配:您不需要围绕被分配事物的括号(通常)。

Also: Are you running PHP4? Your constructor function indicates you are. I'd recommend moving to 5.21 or higher if at all possible as classes and objects are much better. You can also use the rather useful __construct method (as opposed to using the class named method - in your case: Score()). This makes inheritance and extending easier because your classes are no longer having to remember in two places what class they are extending from.

另外:你在运行PHP4吗?你的构造函数表明你是。我建议尽可能移动到5.21或更高,因为类和对象要好得多。您还可以使用相当有用的__construct方法(而不是使用类命名方法 - 在您的情况下:Score())。这使得继承和扩展更容易,因为您的类不再需要在两个地方记住它们从哪个类扩展。

#2


3  

You can only return one value in each function or method.

您只能在每个函数或方法中返回一个值。

In your situation, you should have a method for each of the class members:

在您的情况下,您应该为每个类成员都有一个方法:

public function getScore() {
   return $this->score;
}

public function getName() {
   return $this->name;
}

public function getDept() {
   return $this->dept;
}


public function getDate() {
   return $this->date;
}

Edit after the comments:

评论后编辑:

You could also need a method that returns all the members as a single string:

您还可能需要一个将所有成员作为单个字符串返回的方法:

public function getAll() {
   return $this->getScore(). " " .$this->getName() . " " .$this->getDept(). " " .$this->getDate();
}

#3


1  

First of all you should use public, protected or private instead of var

首先,你应该使用public,protected或private而不是var

var $score;
var $name;
var $dept;
var $date;

such as

protected $score;

or with a coding standard prefix protected/private variables and methods with underscore like such

或者使用编码标准前缀protected / private变量和带有下划线的方法

protected $_score;

This method can also be called __construct

此方法也可以称为__construct

function Score($score, $name, $dept, $date)
{

The var is declared as score but you assign a variable to scores. I also don't understand why you have parentheses around the variable.

var被声明为得分,但您为分数指定了一个变量。我也不明白为什么你在变量周围有括号。

        $this->scores = ($score);
        $this->name = ($name);
        $this->dept = ($dept);
        $this->date = ($date);

Replace with

    $this->score = $score;
    $this->name = $name;
    $this->dept = $dept;
    $this->date = $date;

}

The first return encountered will return that value from the function/method. I suggest you recode to add get/set for each variable, ie getScore() or to use PHP5 method overloading __set, __get and __call.

遇到的第一个返回将从函数/方法返回该值。我建议你重新编写为每个变量添加get / set,即getScore()或使用PHP5方法重载__set,__ get和__call。

public function getScore() {
        return $this->score;
}

}

You can also look at the automatic methods to set and get variables Overloading

您还可以查看设置和获取变量重载的自动方法

#1


2  

You can't return more than once in a function. You could return a concatenated string:

您不能在函数中返回多次。您可以返回连接的字符串:

return $this->scores.' '.this->name.' '.$this->dept.' '.$this->date;
//added spaces for readability, but this is a silly thing to do anyway...

I wouldn't recommend it though as you'd be mixing you presentation of the object with its functionality - don't.

我不推荐它,因为你要将对象的呈现与其功能混合 - 不要。

I'd suggest making a template of some sort (I'm imagining you might want to tabulate this data?). Each row would look something like:

我建议制作某种模板(我想你可能想把这些数据制成表格?)。每行看起来像:

<tr>
  <td><?php echo $score->name; ?></td>
  <td><?php echo $score->scores; ?></td>
  <!-- more cells for more properies? -->
</tr>

and giving it your object or objects in array (you know about foreach{} ?). I know it looks more long-winded, but separating these concerns is going to be better for you in the long run.

并给它你的对象或数组(你知道foreach {}?)。我知道它看起来更加冗长,但从长远来看,将这些问题分开对你来说会更好。

Assigning with = : you don't need the parentheses around the thing being assigned (usually).

使用=分配:您不需要围绕被分配事物的括号(通常)。

Also: Are you running PHP4? Your constructor function indicates you are. I'd recommend moving to 5.21 or higher if at all possible as classes and objects are much better. You can also use the rather useful __construct method (as opposed to using the class named method - in your case: Score()). This makes inheritance and extending easier because your classes are no longer having to remember in two places what class they are extending from.

另外:你在运行PHP4吗?你的构造函数表明你是。我建议尽可能移动到5.21或更高,因为类和对象要好得多。您还可以使用相当有用的__construct方法(而不是使用类命名方法 - 在您的情况下:Score())。这使得继承和扩展更容易,因为您的类不再需要在两个地方记住它们从哪个类扩展。

#2


3  

You can only return one value in each function or method.

您只能在每个函数或方法中返回一个值。

In your situation, you should have a method for each of the class members:

在您的情况下,您应该为每个类成员都有一个方法:

public function getScore() {
   return $this->score;
}

public function getName() {
   return $this->name;
}

public function getDept() {
   return $this->dept;
}


public function getDate() {
   return $this->date;
}

Edit after the comments:

评论后编辑:

You could also need a method that returns all the members as a single string:

您还可能需要一个将所有成员作为单个字符串返回的方法:

public function getAll() {
   return $this->getScore(). " " .$this->getName() . " " .$this->getDept(). " " .$this->getDate();
}

#3


1  

First of all you should use public, protected or private instead of var

首先,你应该使用public,protected或private而不是var

var $score;
var $name;
var $dept;
var $date;

such as

protected $score;

or with a coding standard prefix protected/private variables and methods with underscore like such

或者使用编码标准前缀protected / private变量和带有下划线的方法

protected $_score;

This method can also be called __construct

此方法也可以称为__construct

function Score($score, $name, $dept, $date)
{

The var is declared as score but you assign a variable to scores. I also don't understand why you have parentheses around the variable.

var被声明为得分,但您为分数指定了一个变量。我也不明白为什么你在变量周围有括号。

        $this->scores = ($score);
        $this->name = ($name);
        $this->dept = ($dept);
        $this->date = ($date);

Replace with

    $this->score = $score;
    $this->name = $name;
    $this->dept = $dept;
    $this->date = $date;

}

The first return encountered will return that value from the function/method. I suggest you recode to add get/set for each variable, ie getScore() or to use PHP5 method overloading __set, __get and __call.

遇到的第一个返回将从函数/方法返回该值。我建议你重新编写为每个变量添加get / set,即getScore()或使用PHP5方法重载__set,__ get和__call。

public function getScore() {
        return $this->score;
}

}

You can also look at the automatic methods to set and get variables Overloading

您还可以查看设置和获取变量重载的自动方法