简体   繁体   English

如何从类中实例化$ this类的对象? 的PHP

[英]How to instantiate object of $this class from within the class? PHP

I have a class like this: 我有一个这样的课:

class someClass {

  public static function getBy($method,$value) {
    // returns collection of objects of this class based on search criteria
    $return_array = array();
    $sql = // get some data "WHERE `$method` = '$value'
    $result = mysql_query($sql);
    while($row = mysql_fetch_assoc($result)) {
      $new_obj = new $this($a,$b);
      $return_array[] = $new_obj;
    }
    return $return_array;
  }

}

My question is: can I use $this in the way I have above? 我的问题是:我可以像上面一样使用$ this吗?

Instead of: 代替:

  $new_obj = new $this($a,$b);

I could write: 我可以写:

  $new_obj = new someClass($a,$b);

But then when I extend the class, I will have to override the method. 但是当我扩展类时,我将不得不重写该方法。 If the first option works, I won't have to. 如果第一个选项有效,则无需这样做。

UPDATE on solutions: 解决方案更新:

Both of these work in the base class: 这些都在基类中起作用:

1.) 1.)

  $new_obj = new static($a,$b);

2.) 2.)

  $this_class = get_class();
  $new_obj = new $this_class($a,$b);

I have not tried them in a child class yet, but I think #2 will fail there. 我还没有在儿童班上尝试过它们,但是我认为#2在那里会失败。

Also, this does not work: 另外,这不起作用:

  $new_obj = new get_class()($a,$b);

It results in a parse error: Unexpected '(' It must be done in two steps, as in 2.) above, or better yet as in 1.). 它会导致解析错误:意外的'('必须分两步完成,如上面的2.)所述,或者更好的是如1.)所述。

Easy, use the static keyword 简单,使用static关键字

public static function buildMeANewOne($a, $b) {
    return new static($a, $b);
}

See http://php.net/manual/en/language.oop5.late-static-bindings.php . 参见http://php.net/manual/en/language.oop5.late-static-bindings.php

You may use ReflectionClass::newInstance 您可以使用ReflectionClass :: newInstance

http://ideone.com/THf45 http://ideone.com/THf45

class A
{
    private $_a;
    private $_b;

    public function __construct($a = null, $b = null)
    {
        $this->_a = $a;
        $this->_b = $b;

        echo 'Constructed A instance with args: ' . $a . ', ' . $b . "\n";
    }

    public function construct_from_this()
    {
        $ref = new ReflectionClass($this);
        return $ref->newInstance('a_value', 'b_value');
    }
}

$foo = new A();
$result = $foo->construct_from_this();

Try using get_class(), this works even when the class is inherited 尝试使用get_class(),即使继承了该类,此方法也有效

<?
class Test {
    public function getName() {
        return get_class() . "\n";
    }

    public function initiateClass() {
        $class_name = get_class();

        return new $class_name();
    }
}

class Test2 extends Test {}

$test = new Test();

echo "Test 1 - " . $test->getName();

$test2 = new Test2();

echo "Test 2 - " . $test2->getName();

$test_initiated = $test2->initiateClass();

echo "Test Initiated - " . $test_initiated->getName();

When running, you'll get the following output. 运行时,您将获得以下输出。

Test 1 - Test 测试1-测试

Test 2 - Test 测试2-测试

Test Initiated - Test 测试启动-测试

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

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