dsw8292301 2016-04-13 19:18
浏览 58
已采纳

在PHP中调用函数

I am trying to call a function called displaySentence() and have it output the value of the 'sentence' typed into the form on candycontest.php. The function will eventually have more features, but for now I am just trying to echo out the value of that sentence to ensure the function works. When I run the script, the pages displays until it gets to my function, at which point it is blank.

candycontest.php

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Pete's Treats Candy Contest</title>
</head>
<body>
    <form action="checkticket.php" method="post">
        <label for="ticketNum">Enter your ticket number:</label>
        <input type="number" name="ticketNum" style="width:100px"><br/>
        <label for="sentence">Enter the magic sentence:</label>
        <input type="text" name="sentence" style="width:600px"><br/>
        <input type="submit" value="Am I a Winner?">
    </form>
</body>
</html>

checkticket.php

<?php

$userTicket = $_POST['ticketNum'];


class MagicSentence {
public $sentence;


public function __construct($sentence) {
    $this->setSentence($sentence);
}

public function getSentence() { return $this->sentence; }
public function setSentence($sentence) {
    $this->sentence = $sentence;
}
} // End class MagicSentence



class Ticket extends MagicSentence {
public $ticketNum;

public function displaySentence() {
    $userSentence = $_POST['sentence'];
    echo $userSentence;
}
}

$magicSentence = new MagicSentence("The cow jumped over the moon.");
?>



<html>
<head>
    <meta charset="utf-8">
    <title>Pete's Treats Candy Contest</title>
</head>
<body>
    <?php 


        echo 'Your ticket number is: ' . $userTicket . "<br>";
        echo 'The magic sentence is: ' . $magicSentence->getSentence() .     "<br>";

        displaySentence();

    ?>
</body>
</html>
  • 写回答

3条回答 默认 最新

  • douyu0792 2016-04-13 19:30
    关注

    Change $magicSentence = new MagicSentence("The cow jumped over the moon."); to $magicSentence = new Ticket("The cow jumped over the moon.");

    You need to do this because the displaySentence() method exists under the Ticket class (which extends off the MagicSentence class).

    Also, change displaySentence(); to $magicSentence->displaySentence(); in order to call your method. You cannot call a method as you would a regular function.

    Do that and you should be golden.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料