PHP:在构造函数中使用 $this

发布于 2024-10-18 13:46:22 字数 200 浏览 2 评论 0原文

我有一个在 php 中使用这种语法的想法。它说明了创建对象有不同的后备方法

function __construct() {

   if(some_case())
      $this = method1();
   else
      $this = method2();

}

这是一场噩梦吗?或者它有效吗?

I have an idea of using this syntax in php. It illustrates that there are different fallback ways to create an object

function __construct() {

   if(some_case())
      $this = method1();
   else
      $this = method2();

}

Is this a nightmare? Or it works?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

谜泪 2024-10-25 13:46:22

或者有效吗?

这不起作用。您无法取消设置或从根本上更改在构造函数中创建的对象。您也可以不设置返回值。您所能做的就是设置对象的属性。

解决这个问题的一种方法是拥有一个单独的“工厂”类或函数,它检查条件并返回正确对象的新实例,如下所示:

function factory() {

   if(some_case())
      return new class1();
  else
      return new class2();

}

另请参阅:

Or it works?

It doesn't work. You can't unset or fundamentally alter the object that is being created in the constructor. You can also not set a return value. All you can do is set the object's properties.

One way to get around this is having a separate "factory" class or function, that checks the condition and returns a new instance of the correct object like so:

function factory() {

   if(some_case())
      return new class1();
  else
      return new class2();

}

See also:

萌化 2024-10-25 13:46:22

为什么不做一些更常见的事情,例如:

function __construct() {

   if(some_case())
      $this->construct1();
   else
      $this->construct2();
}

Why not to do something more common like:

function __construct() {

   if(some_case())
      $this->construct1();
   else
      $this->construct2();
}
晨敛清荷 2024-10-25 13:46:22

您可以只创建类方法 method1 和 method2 并编写

function __construct() {

   if(some_case())
      $this->method1();
   else
      $this->method2();

}

You can just create class methods method1 and method2 and just write

function __construct() {

   if(some_case())
      $this->method1();
   else
      $this->method2();

}
为你拒绝所有暧昧 2024-10-25 13:46:22

您可以制作工厂方法。

例子:

class A {}
class B {}

class C {
   function static getObject() {
      if(some_case())
         return new A();
      else
          return new B();
   }
}

$ob = C::getObject();

You can make factory method.

Example:

class A {}
class B {}

class C {
   function static getObject() {
      if(some_case())
         return new A();
      else
          return new B();
   }
}

$ob = C::getObject();
十秒萌定你 2024-10-25 13:46:22

听起来有点像 单例类模式

It sounds a little bit like the Singleton class pattern.

dawn曙光 2024-10-25 13:46:22

请参阅@Ivan 的回复等,了解您想要执行的操作的正确语法。

但是,还有另一种选择 - 使用静态方法作为替代构造函数:(

class myclass {
     function __construct() { /* normal setup stuff here */}

     public static function AlternativeConstructor() {
         $obj = new myclass; //this will run the normal __construct() code
         $obj->somevar = 54; //special case in this constructor.
         return $obj;
     }

}

...

//this is how you would use the alternative constructor.
$myobject = myclass::AlternativeConstructor();

注意:你绝对不能在静态方法中使用 $this

See @Ivan's reply among others for the correct syntax for what it looks like you're trying to do.

However, there are is another alternative - use a static method as an alternative constructor:

class myclass {
     function __construct() { /* normal setup stuff here */}

     public static function AlternativeConstructor() {
         $obj = new myclass; //this will run the normal __construct() code
         $obj->somevar = 54; //special case in this constructor.
         return $obj;
     }

}

...

//this is how you would use the alternative constructor.
$myobject = myclass::AlternativeConstructor();

(note: you definitely can't use $this in a static method)

白昼 2024-10-25 13:46:22

如果你想共享一些功能,请执行一些类似的操作

class base{
    'your class'
}

class A extends base{
    'your class'
}

class B extends base{
    'your class'
}

并调用类似的操作

if(some_case())
    $obj = new A();
else
    $obj = new B();

If you want to share some functions, do some like

class base{
    'your class'
}

class A extends base{
    'your class'
}

class B extends base{
    'your class'
}

And call like

if(some_case())
    $obj = new A();
else
    $obj = new B();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文