douci2015
2013-10-28 22:21
浏览 44
已采纳

PHP隐式调用构造函数

I wrote a class which is initialize with integer (something like wrapper for int):

class MyClass
{
    function __construct ($value)
    {
        // code, code, code
    }
}

I also wrote a function which is accepting the object of this class as the parameter:

class OtherClass
{
    public function foo (MyClass $obj)
    {
        // code, code, code
    }
}

It is possible to simply this call function from:

$bar = new OtherClass();
$bar->foo(new MyClass(17));

to:

$bar = new OtherClass();
$bar->foo(17);

?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douduanque5850 2013-10-28 22:25
    已采纳

    You can do something similar:

    class OtherClass
    {
        public function foo ($value)
        {
            $obj = new MyClass($value)
            // code, code, code
        }
    }
    

    But as others mentioned it is less clear...

    点赞 评论

相关推荐 更多相似问题