Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何创建该方法所属的同一类的实例?_Php_Oop - Fatal编程技术网

Php 如何创建该方法所属的同一类的实例?

Php 如何创建该方法所属的同一类的实例?,php,oop,Php,Oop,在类中是否有包含“this class”的变量/魔术属性,用于new 我想: trait foo { public function __construct( $x ) { // ... do stuff ... } static function init( $x ) { return new __THISCLASS__( $x ); // <--- whuzzat? } ) class A { use fo

在类中是否有包含“this class”的变量/魔术属性,用于
new

我想:

trait foo {

    public function __construct( $x ) {
        // ... do stuff ...
    }

    static function init( $x ) {
        return new __THISCLASS__( $x ); // <--- whuzzat?
    }

)

class A {
    use foo;
}

class B {
    use foo;
}
trait foo{
公共功能构造($x){
//…做事。。。
}
静态函数init($x){

返回新的uuThisClass uuuuu($x);//您可以获得使用trait和函数的类

trait Foo
{
    public static function init()
    {
        $class = get_called_class();
        return new $class();
    }
}


class A {
    use Foo;
}

$obj = A::init();

另一种方法是使用

您可以获得使用trait和function的类

trait Foo
{
    public static function init()
    {
        $class = get_called_class();
        return new $class();
    }
}


class A {
    use Foo;
}

$obj = A::init();

另一种方法是使用

我认为实现这一点最简单、最可读的方法是使用
静态来利用

例如:

it{
公共静态函数构建($x){
返回新的静态数据($x);
}
}
甲级
{
公共功能构造($x){
$this->x=$x;
}
使用它;
}
B类
{
公共功能构造($x){
$this->x=$x*2;
}
使用它;
}
$a=a::构建(2);
$b=b::构建(2);
var_dump($a);
var_dump(b美元);
看到它工作

get\u called\u class()

get_called_class-“后期静态绑定”类名

对于生成器方法,我不会使用
\uuuu CLASS\uuu
self
,因为它们总是引用它们最初使用的类。如果以后扩展该类,该方法将无法按预期工作

例如,类似这样的内容:

trait dangerTrait
{
公共静态函数构建($x){
返回新的自我($x);
}
}
X类
{
使用危险品;
公共功能构造($x){
$this->x=$x*3;
回显“建筑类”、“建筑类”、\n;
}
}
类Y扩展X{
公共功能构造($x){
$this->x=$x*4;
回显“建筑类”、“建筑类”、\n;
}
}
$x=x::构建(2);
$y=y::构建(2);
…将输出:

建筑X类
建筑类别X

$x
$y
都是
x

的实例,我相信实现这一点最简单、最可读的方法是使用
静态
来利用它们

例如:

it{
公共静态函数构建($x){
返回新的静态数据($x);
}
}
甲级
{
公共功能构造($x){
$this->x=$x;
}
使用它;
}
B类
{
公共功能构造($x){
$this->x=$x*2;
}
使用它;
}
$a=a::构建(2);
$b=b::构建(2);
var_dump($a);
var_dump(b美元);
看到它工作

get\u called\u class()

get_called_class-“后期静态绑定”类名

对于生成器方法,我不会使用
\uuuu CLASS\uuu
self
,因为它们总是引用它们最初使用的类。如果以后扩展该类,该方法将无法按预期工作

例如,类似这样的内容:

trait dangerTrait
{
公共静态函数构建($x){
返回新的自我($x);
}
}
X类
{
使用危险品;
公共功能构造($x){
$this->x=$x*3;
回显“建筑类”、“建筑类”、\n;
}
}
类Y扩展X{
公共功能构造($x){
$this->x=$x*4;
回显“建筑类”、“建筑类”、\n;
}
}
$x=x::构建(2);
$y=y::构建(2);
…将输出:

建筑X类
建筑类别X

$x
$y
都是
x

new self()
new static()
的实例,应该可以工作。在正常方法中,您也可以使用
new$this()
。我找到了一些有用的信息供您阅读,
\uuuuu CLASS\uuuuuu
不起作用。
self
起作用。请注意--
self
似乎不会通过扩展类“传播”。请参阅
new self()
new static()
应该可以起作用。在正常方法中,您也可以使用
new$this()
。我找到了一些有用的信息供你阅读,
\uuuu类\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu>不起作用。
self
确实起作用。请注意--
self
似乎不会通过扩展类“传播”。请看
self
vs
静态
self
(或
\uu CLASS\uuuu
)。我错过了什么吗?一点也没有——只是对我自己或其他人有用的注释