search

Home  >  Q&A  >  body text

$s = new Person();This bracket reports an error

为什么这个括号会报错呀

dc4ee2ae65a848eac1d54849c8806c0.png

在学php了在学php了885 days ago863

reply all(9)I'll reply

  • autoload

    autoload2022-07-15 17:23:02

    $variable = '李四';
    $s = new Person($variable);

    That’s it

    reply
    0
  • 在学php了

    Isn't it possible to create a new instance without initializing it?

    在学php了 · 2022-07-15 17:27:48
    在学php了

    I copied a piece of code on csdn, and an error was reported in my own parentheses. I was puzzled for a long time.

    在学php了 · 2022-07-15 17:29:10
    在学php了

    https://blog.csdn.net/weixin_34152820/article/details/91883654?ops_request_misc=%7B%22request%5Fid%22%3A%22165787695116782184680934%22%2C%22scm%22%3A%2220140713.13 0102334..%22%7D&request_id =165787695116782184680934&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduend~default-1-91883654-null-null.142^v32^pc_search_v2,185^v2^control&utm_term=php access control&spm= 1018.2226. 3001.4187 This is this article, he created an uninitialized instance

    在学php了 · 2022-07-15 17:30:01
    autoload

    Take a look at the reply below. It’s preset in the constructor, so it must be there. Just don’t preset it.

    autoload · 2022-07-16 15:40:56
    autoload

    His new one is Student, not Person, you can see clearly for yourself

    autoload · 2022-07-16 15:42:40
    在学php了

    Yes, there was something wrong with the parentheses when I was a new student. It was like this at first, but I found it after I posted this question. Public function __construct($name="") can be solved in this way. I just want to know if $s = new Student(); is wrong in not initializing this example.

    在学php了 · 2022-07-18 09:05:57
  • autoload

    autoload2022-07-15 17:19:48

    image.png

    The constructor has formal parameters, and when creating a new object, it takes actual parameters

    reply
    0
  • 在学php了

    Public function __construct($name="") will do it

    在学php了 · 2022-07-15 17:26:25
  • Cancelreply