将类作为参数传递

时间:2015-05-11 19:03:26

标签: php oop

我试图将一个类作为参数传递,但我不知道是否可能。

class User {
    var $name;
}

class UserRepository {
  private $type;
  public function __construct(Class) {
     $this->type = Class;
  }

  public function getInstance() {
     return new $this->type;
  }
}

$obj = new UserRepository(User);

我也接受其他方法的建议。

2 个答案:

答案 0 :(得分:3)

只需实例化该类并调用该

:

另一个选项(因为$user = new User(); $obj = new UserRepository($user); 仅包含变量)是使变量User并使用

static

答案 1 :(得分:1)

我认为你只是在寻找一个字符串:

<lst name="explain">
    <str name="15318_1">
2.53099786E10 = (MATCH) sum of:
  1.26549893E10 = (MATCH) sum of:
    0.07344055 = (MATCH) MatchAllDocsQuery, product of:
      0.07344055 = queryNorm
    1.26549893E10 = (MATCH) weight(text:reinigungsprodukte in 5284) [DefaultSimilarity], result of:
      1.26549893E10 = score(doc=5284,freq=1.0 = termFreq=1.0
), product of:
        0.70328265 = queryWeight, product of:
          9.576217 = idf(docFreq=1, maxDocs=10608)
          0.07344055 = queryNorm
        1.79941724E10 = fieldWeight in 5284, product of:
          1.0 = tf(freq=1.0), with freq of:
            1.0 = termFreq=1.0
          9.576217 = idf(docFreq=1, maxDocs=10608)
          1.87904819E9 = fieldNorm(doc=5284)
    0.07344055 = (MATCH) MatchAllDocsQuery, product of:
      0.07344055 = queryNorm
  1.26549893E10 = (MATCH) weight(text:reinigungsprodukte in 5284) [DefaultSimilarity], result of:
    1.26549893E10 = score(doc=5284,freq=1.0 = termFreq=1.0
), product of:
      0.70328265 = queryWeight, product of:
        9.576217 = idf(docFreq=1, maxDocs=10608)
        0.07344055 = queryNorm
      1.79941724E10 = fieldWeight in 5284, product of:
        1.0 = tf(freq=1.0), with freq of:
          1.0 = termFreq=1.0
        9.576217 = idf(docFreq=1, maxDocs=10608)
        1.87904819E9 = fieldNorm(doc=5284)
</str>
    <str name="445_1">
0.04896037 = (MATCH) product of:
  0.09792074 = (MATCH) sum of:
    0.09792074 = (MATCH) product of:
      0.1468811 = (MATCH) sum of:
        0.07344055 = (MATCH) MatchAllDocsQuery, product of:
          0.07344055 = queryNorm
        0.07344055 = (MATCH) MatchAllDocsQuery, product of:
          0.07344055 = queryNorm
      0.6666667 = coord(2/3)
  0.5 = coord(1/2)
</str>

输出:

  

object(User)#2(1){[“name”] =&gt; NULL}

An example