可以单独启动2个PHP脚本吗?

时间:2015-10-21 14:18:39

标签: php static-methods

说我有这个班级

class Hello
{
    /**
     * Construct won't be called inside this class and is uncallable from
     * the outside. This prevents instantiating this class.
     * This is by purpose, because we want a static class.
     */
    private function __construct() {}
    private static $greeting = 'Hello';
    private static $initialized = false;

    private static function initialize()
    {
        if (self::$initialized)
            return;

        self::$greeting .= ' There!';
        self::$initialized = true;
    }

    public static function greet()
    {
        self::initialize();
        echo self::$greeting;
    }

    public static function changeGreet($new)
    {
        self::initialize();
        self::$greeting = $new;
    }
}

我有两个脚本,一个是我在命令行中运行的脚本。

cmdLine.php php cmdLine.php

for($i = 0; $i < 25;$i++){
    echo Hello::greet() . PHP_EOL;
    sleep(5);
}

我在浏览器中加载了一个。

browser.php

Hello::changeGreet('NewGreet');

我想,在第一次运行cmdLine.php之后。然后加载browser.php,问候语将在下次循环运行时更改,但它没有。

这可以用PHP做什么?

1 个答案:

答案 0 :(得分:1)

这可能与PHP有关吗?:不。

这需要某种中介才能发挥作用。需要一个文件,数据库或其他东西。

(例如)

initialize()中,您可以使用file_get_contents()打开文件,然后在需要更改时将问候语存储为file_put_contents()

相关问题