dqp4933 2011-06-13 01:49
浏览 62
已采纳

PHP:$ myObject = new ClassName(); vs newClassName;

So, I understand that sometimes people use parens and sometimes they don't when declaring an instance of a class as an object.

If I declare a class without arguments and then extend it to have arguments, will the initial class need to have had parens?

Furthermore, can I have a class with brackets and no specific parameter or reference variable declaration like this:

$myObject = clone ClassName();

And then use parameters for the class....

class User($param, $param2)

Or do classes ever even use parameters? I'm not really sure because there isn't an example of a class that takes parameters in my book....

  • 写回答

2条回答 默认 最新

  • dqlk31541 2011-06-13 01:57
    关注

    Those parameters, eg

    $myObject = new ClassAwesome($param1, $param2);
    

    dont correspond to the class declaration, they relate to the constructor. An example:

    //this is how you declare a class, there will not be a parenthesis here
    class ClassAwesome {
    
        //here is where the parameters are used when you instantiate a class
        public function __construct($param1, $param2) {
            //init code goes here
        }
    }
    

    So basically the reason you dont see a class declaration with parameters is because they dont exist, the class constructor is where the parameters go.

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

报告相同问题?

悬赏问题

  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答