简体   繁体   English

我如何实例化具有私有构造函数的 class

[英]How can i instantiate a class which is having private constructor

How can i instantiate a class which is having private constructor.?如何实例化具有私有构造函数的 class ?

I don't want to use any function inside the class to create its own instance.我不想在 class 中使用任何 function 来创建自己的实例。

Ex class is:例如 class 是:

class Test extends Test2 implements Test3 {
   private function __construct () {
   }

   function doDisplay() {
   }

   function Docall() {
   }
}

You can instanciate it using Reflection (PHP >= 5.4)您可以使用反射来实例化它(PHP >= 5.4)

class Test {

    private $a;

    private function __construct($a)
    {
        $this->a = $a;
    }
}

$class = new ReflectionClass(Test::class);
$constructor = $class->getConstructor();
$constructor->setAccessible(true);
$object = $class->newInstanceWithoutConstructor();
$constructor->invoke($object, 1);

It works, but keep in mind that this was not an usage that the class was designed for, and that it might have some unforeseen side effects.它有效,但请记住,这不是 class 设计的用途,它可能会产生一些无法预料的副作用。

You can't invoke a private constructor from anywhere but within the class itself, so you have to use an externally-accessible static method to create instances.除了 class 本身之外,您不能从任何地方调用私有构造函数,因此您必须使用外部可访问的 static 方法来创建实例。

Also, if Test2 has a constructor that isn't private, you can't make Test::__construct() private.此外,如果Test2有一个非私有的构造函数,则不能将Test::__construct()私有。

You cannot instantiate an object from a class whose constructor is private (out of syllabus).您不能从其构造函数为private (超出教学大纲)的 class 实例化 object。

If you are intend to use it's function, then you should have them as static and use them to fulfill your needs.如果您打算使用它的 function,那么您应该将它们作为static并使用它们来满足您的需求。 Make sure that function is public.确保 function 是公开的。

For ex:例如:

<?php

class something
{
    private function __construct()
    {
        echo "something";
    }

    public static function display($msg)
    {
        echo $msg;
    }
}

something::display("Testing...");

?>

See the working example: http://codepad.org/VoYeyk8W请参阅工作示例: http://codepad.org/VoYeyk8W

Whoever designed this class does not want you to instantiate one directly, you cannot do what you want to do.设计这个 class 的人不希望你直接实例化一个,你不能做你想做的事。 Most likely the original author had a good reason for doing this, maybe it was memory management or he wanted to control the life cycle or stop problems with threading, it could be a lot of reasons.很可能原作者这样做是有充分理由的,也许是 memory 管理或者他想控制生命周期或停止线程问题,这可能是很多原因。

The only obvious answer is to rewrite the sources so they work the way you want instead of trying to break into someone else's design.唯一明显的答案是重写源代码,使它们按照您想要的方式工作,而不是试图闯入别人的设计。 Or you could build a wrapper around this class that hides away the part you don't like so the rest of you code doesn't have to know.或者你可以围绕这个 class 构建一个包装器,它隐藏你不喜欢的部分,所以你的代码中的 rest 不必知道。

Well, if you don't want to use a (static) method inside the class to create an instance I guess the answer to your question simply is: you cannot.好吧,如果您不想在 class 中使用(静态)方法来创建实例,我想您的问题的答案就是:您不能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM