我是php中OO的新手。我在处理这段代码时,出现了一个错误"类年份的对象不能在字符串中转换"。现在我知道错误是怎么说的,但我无法找出解决方案。是的,我已经检查了所有其他关于这个的问题。有人请帮帮我。这是代码:
<?php
if(isset($_POST['sub'])){
$name=$_POST['name'];
$age=$_POST['age'];
$hrs=$_POST['hrs'];
class Years
{
const divid=24;
public function __construct($nme,$ag,$hr)
{
$ans= ($ag * $hr)/self::divid;
return $ans;
}
public function calc()
{
return "ok";
}
}
echo $yrs= new Years($name,$age,$hrs);
}
?>
<html>
<head>
<title>Form</title>
</head>
<body>
<h1>My Unconcious Life</h1>
<form method="post">
Your Name:<br />
<input type="text" name="name" /><br />
Your Age:<br />
<input type="text" name="age" /><br />
Hours slept per night:<br />
<input type="text" name="hrs" /><br />
<input type="submit" name="sub" value="Calculate" />
</form>
</body>
</html>
您的类存在几个问题。让我们看看:
class Years
{
const divid=24;
public function __construct($nme,$ag,$hr)
{
$ans= ($ag * $hr)/self::divid;
return $ans;
}
public function calc()
{
return "ok";
}
}
echo $yrs= new Years($name,$age,$hrs);
问题1:构造函数不返回任何内容。
为了让你的类返回一些东西,你应该创建一个属性,然后用getter方法返回:
class Years
{
const divid=24;
private $ans;
public function __construct($nme,$ag,$hr)
{
$this->ans = ($ag * $hr)/self::divid;
}
public function getAns()
{
return $this->ans;
}
public function calc()
{
return "ok";
}
}
问题2:您的构造函数有未使用的参数。
如果不需要$nme
(注意拼写错误)参数,为什么要将其传递给构造函数?
class Years
{
const divid=24;
private $ans;
public function __construct($ag,$hr)
{
$this->ans = ($ag * $hr)/self::divid;
}
public function getAns()
{
return $this->ans;
}
public function calc()
{
return "ok";
}
}
问题3:为了将对象转换为字符串,类应该实现__toString()
方法:
class Years
{
const divid=24;
private $ans;
public function __construct($ag,$hr)
{
$this->ans = ($ag * $hr)/self::divid;
}
public function getAns()
{
return $this->ans;
}
public function __toString()
{
return $this->getAns();
}
public function calc()
{
return "ok";
}
}
尽管如此,我想说,你的根本问题是,你在不需要的时候创建对象。
如果你只想转换时间单位,你只需要一个函数:
function getYears($ag, $hr) {
return $ag * $hr / 24;
}
这就是它应该做的。这不是一个面向对象的问题,一个简单的函数调用就能解决。
永远记得KISS。
与其他答案略有不同。稍微容易一点。
class Years
{
const divid=24;
public $ans;
public function __construct($nme,$ag,$hr)
{
$this->ans = ($ag * $hr)/self::divid;
}
public function calc()
{
return "ok";
}
}
$yrs= new Years($name,$age,$hrs);
echo $yrs->ans;
正确的编码方式是
<?php
class Years
{
private $divid;
// although this is
public function __construct($div)
{
$this->divid = $div;
}
public function calc($ag,$hr)
{
return ($ag * $hr) / $this->divid;
}
}
if( isset($_POST['sub'], $_POST['age'], $_POST['hrs'], $_POST['name']) ){
$name=$_POST['name'];
$age=$_POST['age'];
$hrs=$_POST['hrs'];
$yrs = new Years(24);
$result = $yrs->calc($age,$hrs)
echo "The result is $result";
} else {
?>
<!DOCTYPE html>
<html>
<head>
<title>Form</title>
</head>
<body>
<h1>My Unconcious Life</h1>
<form method="post">
Your Name:<br />
<input type="text" name="name" /><br />
Your Age:<br />
<input type="text" name="age" /><br />
Hours slept per night:<br />
<input type="text" name="hrs" /><br />
<input type="submit" name="sub" value="Calculate" />
</form>
</body>
</html>
<?php
}
?>
?>