doudengshen5591 2015-01-02 21:29
浏览 34
已采纳

PHP类::为什么要声明为新的? [关闭]

I'm very new to php classes and I was wonder why do I need to declare it to a variable and set it as NEW?

Here is an example :

class myFirstClass {
    function Version(){
        return 'Version 1';
    }
    function Info(){
        return 'This class is doing nothing';
    }
}

$hola = new myFirstClass;

echo $hola->Version();

Why this won't work WITHOUT declare it to a variable and set it to NEW ?

In other words... Without this line :

$hola = new myFirstClass;

I'm so used to functions so it looks weird to me...

  • 写回答

4条回答 默认 最新

  • dor2p0520 2015-01-02 21:41
    关注

    You are right! It is not necessary to use the new operator:

    class myFirstClass {
        static function Version(){// static keyword
            return 'Version 1';
        }
        function Info(){
            return 'This class is doing nothing';
        }
    }
    
    echo myFirstClass::Version();// direct call
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置