问题描述
我想在PHP中创建一个静态类,并像在C#中一样行事,所以
- 构造函数会自动在第一个呼叫中呼叫
- 无需实例化
这种东西...
static class Hello { private static $greeting = 'Hello'; private __construct() { $greeting .= ' There!'; } public static greet(){ echo $greeting; } } Hello::greet(); // Hello There!
推荐答案
您可以在PHP中有静态类,但它们不会自动调用构造函数(如果您尝试调用self::__construct(),您会遇到错误).
因此,您必须创建一个initialize()函数并在每种方法中调用:
<?php class Hello { 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; } } Hello::greet(); // Hello There! ?>
其他推荐答案
除了格雷格的答案外,我还建议将构造函数设置为私有,以便不可能实例化课程.
因此,以我的拙见,这是一个基于格雷格的示例:
<?php 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; } } Hello::greet(); // Hello There! ?>
其他推荐答案
您可以拥有类似"静态"的类.但是我想,确实缺少一些重要的东西:在PHP中,您没有应用程序周期,因此您在整个应用程序中都不会获得真正的静态(或单身)...
请参阅 php
中的单身人士问题描述
I want to create a static class in PHP and have it behave like it does in C#, so
- Constructor is automatically called on the first call to the class
- No instantiation required
Something of this sort...
static class Hello { private static $greeting = 'Hello'; private __construct() { $greeting .= ' There!'; } public static greet(){ echo $greeting; } } Hello::greet(); // Hello There!
推荐答案
You can have static classes in PHP but they don't call the constructor automatically (if you try and call self::__construct() you'll get an error).
Therefore you'd have to create an initialize() function and call it in each method:
<?php class Hello { 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; } } Hello::greet(); // Hello There! ?>
其他推荐答案
In addition to Greg's answer, I would recommend to set the constructor private so that it is impossible to instantiate the class.
So in my humble opinion this is a more complete example based on Greg's one:
<?php 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; } } Hello::greet(); // Hello There! ?>
其他推荐答案
you can have those "static"-like classes. but i suppose, that something really important is missing: in php you don't have an app-cycle, so you won't get a real static (or singleton) in your whole application...
see Singleton in PHP