Php 类被实例化为对象后,我可以传递_构造参数吗?
我有一个类似的代码片段,如下所示Php 类被实例化为对象后,我可以传递_构造参数吗?,php,oop,Php,Oop,我有一个类似的代码片段,如下所示 class Search { public function search($for, $regEx, $flag) //I would like this to be the constructor { // logic here return $this; } } $this->search($params); // I have my methods chained, so I could u
class Search
{
public function search($for, $regEx, $flag) //I would like this to be the constructor
{
// logic here
return $this;
}
}
$this->search($params);
// I have my methods chained, so I could use it in one line like
// $this->search($params)->hasResults();
if ($this->search->hasResults()) {
echo 'found stuff';
} else {
echo 'didn't find anything';
}
$this->search->search($params);
if ($this->search->hasResults()) {
echo 'found stuff';
} else {
echo 'didn't find anything';
}
public function __call($name, $params)
{
$call = ucfirst($name);
$this->$name = new $call($params);
}
然后我有另一个类,它从中创建了一个对象,然后尝试使用该对象
class MyClass
{
public function start()
{
$this->search = new Search();
}
public function load()
{
$this->search($for, $regEx, $flag);
}
}
我的问题是,是否可以先创建一个对象,然后给它参数
我知道有一些方法可以解决这个问题,但我问这个问题只是因为我想使用这样的对象
class Search
{
public function search($for, $regEx, $flag) //I would like this to be the constructor
{
// logic here
return $this;
}
}
$this->search($params);
// I have my methods chained, so I could use it in one line like
// $this->search($params)->hasResults();
if ($this->search->hasResults()) {
echo 'found stuff';
} else {
echo 'didn't find anything';
}
$this->search->search($params);
if ($this->search->hasResults()) {
echo 'found stuff';
} else {
echo 'didn't find anything';
}
public function __call($name, $params)
{
$call = ucfirst($name);
$this->$name = new $call($params);
}
按照我现在设置的方式,我需要像这样使用它
class Search
{
public function search($for, $regEx, $flag) //I would like this to be the constructor
{
// logic here
return $this;
}
}
$this->search($params);
// I have my methods chained, so I could use it in one line like
// $this->search($params)->hasResults();
if ($this->search->hasResults()) {
echo 'found stuff';
} else {
echo 'didn't find anything';
}
$this->search->search($params);
if ($this->search->hasResults()) {
echo 'found stuff';
} else {
echo 'didn't find anything';
}
public function __call($name, $params)
{
$call = ucfirst($name);
$this->$name = new $call($params);
}
我有一个名为search的方法,它执行逻辑,我不想在命名中重复,也不想更改方法的名称
我知道另一种保持视觉吸引力的方法我可以这样传递一个变量
$search=$this->search->search$params
然后
$search->hassresults
同时,我试图向自己介绍新的OOP概念,并从中学习。这需要通过引用传递东西吗?或者设置某种魔法方法?是的,你可以
class Example {
public $any;
function __counstruct($parameters,$some_text) {
$this->any=$some_text;
return $this->any;
}
}
您可以调用构造函数:
$obj = new Example (true,'hello');
echo $obj->any;
$obj->__construct(true,'bye-bye');
echo $obj->any;
是的,这是可能的
请参见下面的示例
<?php
class a{
public $a = 5;
public function __construct($var){
$this->a = $var;
}
}
$delta = new a(10);
echo $delta->a."\n";
$delta->__construct(15);
echo $delta->a."\n";
我能够通过使用uu call magic方法创建我想要的可视化编码,就像这样
class Search
{
public function search($for, $regEx, $flag) //I would like this to be the constructor
{
// logic here
return $this;
}
}
$this->search($params);
// I have my methods chained, so I could use it in one line like
// $this->search($params)->hasResults();
if ($this->search->hasResults()) {
echo 'found stuff';
} else {
echo 'didn't find anything';
}
$this->search->search($params);
if ($this->search->hasResults()) {
echo 'found stuff';
} else {
echo 'didn't find anything';
}
public function __call($name, $params)
{
$call = ucfirst($name);
$this->$name = new $call($params);
}
从那里我可以用这个
$this->test->search($params);
$this->test->search->hasResults();
当然,我现在将搜索方法设置为类构造函数,而之前的Anwser显示您可以,我不会使用它,因为它打破了封装的概念。实现你想要的目标的正确方法如下
class Search
{
public function __constructor($for='', $regEx='', $flag='')
{
$this->Setup($for, $regEx, $flag);
}
public function Setup($for, $regEx, $flag)
{
//assign params
//clear last result search
//chain
return $this;
}
public function search()
{
// logic here
return $this;
}
}
通过这种方式,您可以重用对象并在构造函数中使用参数,而不会破坏封装。执行上述代码并进行检查。它在5.3.x版本的php中工作