从另一个文件定义的 PHP class 不起作用

Defined PHP class from another file not working

我目前正在学习 PHP,但我 运行 遇到了问题。

我有一个 class 可以让你创建一个活动,set/get 它的名称、日期、描述和价格。它当前位于名为 "class_lib.php".

的文件中
<?php 
class Eventitem
{
    private $eventName;
    private $eventDate;
    private $eventDesc;
    private $eventPrice;

    function __construct()
    {
        $this->eventName = "BLANK";
        $this->eventDate = "January 1";
        $this->eventDesc = "...";
        $this->eventPrice = "[=12=].00";
    }

    public function setName($name)
    {
        $this->eventName = $name;
    }

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

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

    public function getDesc()
    {
        return $this->eventDesc;
    }

    public function getPrice()
    {
        return $this->eventPrice;
    }
}
?>

现在,在我用作测试平台的另一个名为 "blank.php" 的文件中,我尝试定义 class 并使用其 setName 和 getName 方法:

<head>
</head>

<body>

    <div id="content">
        <?php include 'class_lib.php'; ?>
        <?php
            $event = new Eventitem();
            $event->setName("Some Event");

            echo $event->getName();

        ?>
    </div>
</body>

<footer>
</footer>

但是,当我执行它时,没有任何显示!我做错了什么?

我已经将您的代码复制并粘贴到我的本地主机上,并且运行良好。

你这里有一个问题 运行 php 脚本通过你试图使用的任何网络服务器。确保您可以获得任何 php 脚本 运行,例如 <?php phpinfo(); ?>.