首先,请阅读并不要在心跳中投下反对票。
我肯定有大量关于如何使用类之类的示例,但是我仍然无法弄清楚它在php中是如何工作的,作为一个新手,我需要我的示例来帮助我,真的很明白。
我具有此功能
class CaloriesOutput
public function equationCaloriesMaleOutput(){
$contentDto = new \ContentDto();
$weight = $contentDto->getWeight();
$height = $contentDto->getHeight();
$age = $contentDto->getAge();
$equationGainCaloriesMale = ((10 * $weight) + (6.25 * $height) + (5 * $age) + 5) + 250;
$equationLoseCaloriesMale = ((10 * $weight) + (6.25 * $height) + (5 * $age) + 5) - 300;
echo '<br /> To Gain Weight you would need around: ' . $equationGainCaloriesMale . 'Calories';
echo '<br /> To Lose Weight you would need around: ' . $equationLoseCaloriesMale . 'Calories';
echo '<br /><h6>Note: No activity is beeing take into count. Please <a href="https://www.calculator.net/calorie-calculator.html">go here</a> for More information.</h6>';
}
我是index.php
这就是我试图称呼它的方式。
try{
$caloriesOutPutMale = new CaloriesOutput();
$caloriesOutPutMale->equationCaloriesMaleOutput();
}catch (Exception $exception){
echo $exception->getMessage();
}
if (isset($_POST['gender'])){
if ($_POST['gender'] == 1){
}else if ($_POST['gender'] == 2){
echo '<br /> To Gain Weight you would need around: ' . $this->$caloriesOutPutMale . 'Calories';
}else{
return false;
}
}
我的逻辑在哪里有问题?
K:\ xxamp \ htdocs \ index.php:171堆栈跟踪:#0 {main}放在第171行的K:\ xxamp \ htdocs \ index.php中
第171行为$caloriesOutPutMale = new CaloriesOutput();
先谢谢您。请帮助新手:/
答案 0 :(得分:1)
如果准确地复制了它,则您的类内部似乎有语法错误。
PHP中的类的快速概述是它是相关函数的集合。您需要将类的所有功能都包装在{}
内,就像包装单个功能的逻辑一样。因此,如果您执行以下操作,则它应该可以工作:
class CaloriesOutput {
public function equationCaloriesMaleOutput(){
$contentDto = new \ContentDto();
$weight = $contentDto->getWeight();
$height = $contentDto->getHeight();
$age = $contentDto->getAge();
$equationGainCaloriesMale = ((10 * $weight) + (6.25 * $height) + (5 * $age) + 5) + 250;
$equationLoseCaloriesMale = ((10 * $weight) + (6.25 * $height) + (5 * $age) + 5) - 300;
echo '<br /> To Gain Weight you would need around: ' . $equationGainCaloriesMale . 'Calories';
echo '<br /> To Lose Weight you would need around: ' . $equationLoseCaloriesMale . 'Calories';
echo '<br /><h6>Note: No activity is beeing take into count. Please <a href="https://www.calculator.net/calorie-calculator.html">go here</a> for More information.</h6>';
}
}
正如一些评论者所提到的,此代码未遵循OOP最佳做法。 OOP背后的想法是,您可以将代码重用于许多不同的事物,因此很少应进行硬编码。但是,这超出了此问题的范围。最终,几乎任何IDE都可能会发现此错误,因此这并不是Stack Overflow的最佳问题。