PHP-如何告诉对象谁是它的创建者?

PHP-如何告诉对象谁是它的创建者?,php,object,refactoring,Php,Object,Refactoring,嗨 基本上,我要求你告诉我的是如何将“父”引用放入对象中 我需要它来制作“使用方法对象提取方法”的示例,这是Java或C中最常用的重构之一# 在Java中,对“父对象”的引用如下所示: class someClass { MyObject myObj = new MyObject(this); } 就这样:) 但我不知道,如何在PHP中实现同样的功能 如果这是不可能的,您可以告诉我如何将您的方法从类中提取到新的类中,这必须做那个方法所做的事情 换句话说 1-我有一个大而难读的类/重构方法。

基本上,我要求你告诉我的是如何将“父”引用放入对象中

我需要它来制作“使用方法对象提取方法”的示例,这是Java或C中最常用的重构之一#

在Java中,对“父对象”的引用如下所示:

class someClass {
  MyObject myObj = new MyObject(this);
}
就这样:)

但我不知道,如何在PHP中实现同样的功能

如果这是不可能的,您可以告诉我如何将您的方法从类中提取到新的类中,这必须做那个方法所做的事情

换句话说

1-我有一个大而难读的类/重构方法。
2-我将该方法提取到新类中,给它一些字段来代替参数和“execute”之类的方法,以处理该类必须为我做的所有事情。

3-我把我的类的对象放在我的旧函数类中,我调用它的方法“execute”-这样我的大方法中的所有逻辑都完成了。

最简单的方法是在构造函数中传递对父对象的引用,尽管我可能误解了你的目标

class myClass {
  private $parent = false;
  function __construct ($parent=false) {
    $this->parent = $parent;
  }
}
如果要扩展基类,可以使用父类操作符:

class otherClass {
   function someMethod() {
       return 1;
   }
}
class myClass extends otherClass {
   function aMethod() {
      // parent keyword here would refer to "otherClass"
      return $this->someMethod();
  }
}

查看
parent

上的文档最简单的方法是在构造函数中传递对父对象的引用,尽管我可能误解了您的目标

class myClass {
  private $parent = false;
  function __construct ($parent=false) {
    $this->parent = $parent;
  }
}
如果要扩展基类,可以使用父类操作符:

class otherClass {
   function someMethod() {
       return 1;
   }
}
class myClass extends otherClass {
   function aMethod() {
      // parent keyword here would refer to "otherClass"
      return $this->someMethod();
  }
}

查看
parent
上的文档:

您尝试过使用$this关键字吗?

您尝试过使用$this关键字吗?

最好的方法是继承,在继承中调用
parent
关键字访问父类,如下所示:

class Child extends Father
{
    public function __construct()
    {
        parent::__construct();
    }
}

class Father
{
    public function __construct()
    {
       echo "Father says hello";
    }
}

new Child();
使用
parent
keyowrd,您可以像这样调用构造函数
parent::\uu construct()

例如:

如果你在看注射,那么你可以这样做

class Master
{
    private $Slave;

    public function __construct(Slave $Slave)
    {
         $this->Slave = $Slave;
    }
}

$Master = new Master(new Slave);
如果您不确定应该传入的对象,但您知道它应该有一个特定的接口/方法集,那么您可以变得更复杂一些,并执行如下操作:

class Master
{
    private $Slave;

    public function __construct(ISlave $Slave)
    {
         $this->Slave = $Slave;
    }
}

interface ISlave
{
    //Declaration of methods
}

class SomeSlaveObject implements ISlave{}

$Master = new Master(new SomeSlaveObject);

最好的方法是继承,在继承中调用
parent
关键字访问父类,如下所示:

class Child extends Father
{
    public function __construct()
    {
        parent::__construct();
    }
}

class Father
{
    public function __construct()
    {
       echo "Father says hello";
    }
}

new Child();
使用
parent
keyowrd,您可以像这样调用构造函数
parent::\uu construct()

例如:

如果你在看注射,那么你可以这样做

class Master
{
    private $Slave;

    public function __construct(Slave $Slave)
    {
         $this->Slave = $Slave;
    }
}

$Master = new Master(new Slave);
如果您不确定应该传入的对象,但您知道它应该有一个特定的接口/方法集,那么您可以变得更复杂一些,并执行如下操作:

class Master
{
    private $Slave;

    public function __construct(ISlave $Slave)
    {
         $this->Slave = $Slave;
    }
}

interface ISlave
{
    //Declaration of methods
}

class SomeSlaveObject implements ISlave{}

$Master = new Master(new SomeSlaveObject);

示例的PHP版本:

  class someClass {
    public function createObject() {
        $myObj = new MyObject($this);
    }
  }
与java没有太大的不同,只是类型更少,符号更多


“parent”是php中的一个关键字,与java“super”类似,因此您的问题有点令人困惑。

您的示例的php版本:

  class someClass {
    public function createObject() {
        $myObj = new MyObject($this);
    }
  }
与java没有太大的不同,只是类型更少,符号更多

“parent”在php中是一个关键字,就像java中的“super”一样,所以你的问题有点让人困惑。

我试过使用“$this”,但我公司的“php guy”告诉我,我不能使用不引用当前类的字段或函数的$this(它不能引用整个类)

我的代码如下所示:

class someClass {
  MyObject myObj = new MyObject(this);
}
要重构的类:

class AccountBeforeRefactoring {
    // (...)
    public function makeTransfer($amount, $destinationAccount){
        $transferFee = 1;
        if ($amount > 1000){
            $transferFee = 1 + $amount * 0.0001;            
        }

        $this->connectToElixir();

        // (...)         
        // (...)         
        // (...)

        $this->debit($amount + $transferFee);
    }   
}
它变成: 类提取-这是我的方法:

    class TransferMaker {

    private $account;
    private $amount;
    private $destinationAccount;
    private $transferFee;

    public function __construct($source, $amount, $destinationAccount){
        $this->account = $source;
        $this->amount = $amount;
        $this->destinationAccount = $destinationAccount;
        $this->transferFee = 1;
    }

    public function make(){        
        if ($this->amount > 1000){
            $this->transferFee = 1 + $this->amount * 0.0001;            
        }

        $this->account->connectToElixir();

        // (...)         
        // (...)         
        // (...)

        $this->account->debit($this->amount + $this->transferFee);
    }

}
那里的构造方法正确吗?

现在我需要,在我的Account类中创建MakeTransfer对象——我用这种方式尝试过——可以吗

    class Account {
    // (...)

    public function makeTransfer($amount, $destinationAccount){
        new TransferMaker($this, $amount, 
                $destinationAccount).make();
    }
}
再说一次,我能像这样做吗?;) 我的代码能用吗?它确实在Eclipse中编译,但这可能意味着没有。

我尝试使用“$this”,但我公司的“php guy”告诉我,我不能使用不引用当前类的字段或函数的$this(它不能引用整个类)

我的代码如下所示:

class someClass {
  MyObject myObj = new MyObject(this);
}
要重构的类:

class AccountBeforeRefactoring {
    // (...)
    public function makeTransfer($amount, $destinationAccount){
        $transferFee = 1;
        if ($amount > 1000){
            $transferFee = 1 + $amount * 0.0001;            
        }

        $this->connectToElixir();

        // (...)         
        // (...)         
        // (...)

        $this->debit($amount + $transferFee);
    }   
}
它变成: 类提取-这是我的方法:

    class TransferMaker {

    private $account;
    private $amount;
    private $destinationAccount;
    private $transferFee;

    public function __construct($source, $amount, $destinationAccount){
        $this->account = $source;
        $this->amount = $amount;
        $this->destinationAccount = $destinationAccount;
        $this->transferFee = 1;
    }

    public function make(){        
        if ($this->amount > 1000){
            $this->transferFee = 1 + $this->amount * 0.0001;            
        }

        $this->account->connectToElixir();

        // (...)         
        // (...)         
        // (...)

        $this->account->debit($this->amount + $this->transferFee);
    }

}
那里的构造方法正确吗?

现在我需要,在我的Account类中创建MakeTransfer对象——我用这种方式尝试过——可以吗

    class Account {
    // (...)

    public function makeTransfer($amount, $destinationAccount){
        new TransferMaker($this, $amount, 
                $destinationAccount).make();
    }
}
再说一次,我能像这样做吗?;)
我的代码能用吗?它确实在Eclipse中编译,但这可能意味着没有。

如果您谈论的是继承父方法的子对象,那么您可以使用子对象中的
parent::method()
访问父对象的方法。如果您谈论的是继承父方法的子对象,然后,您可以在子对象中使用
parent::method()
访问父对象的方法。您只需使用
$this->someMethod()
,因为
myClass
不包含相同的方法,它会从子类调用继承哪个构造函数?通常不需要手动调用构造函数,而是在实例化类时自动调用构造函数。因此,当我执行
$class=newmyclass()
时,将调用
\u构造
方法。要调用父构造函数,
parent::\u construct()
。看看医生@罗伯特皮特:没错。。。匆忙输入的示例是有缺陷的。updateing.you只需使用
$this->someMethod()
,因为
myClass
不包含相同的方法,它会从子类调用继承哪个构造函数?通常不需要手动调用构造函数,而是在实例化类时自动调用构造函数。因此,当我执行
$class=newmyclass()
时,将调用
\u构造
方法。要调用父构造函数,
parent::\u construct()
。看看医生@罗伯特皮特:没错。。。匆忙输入的示例是有缺陷的。更新。我不知道这是不是最好的方法。毕竟,在大多数情况下,你应该更喜欢组合而不是继承。因此,虽然这是一个有效的选项,但它实际上取决于OP到底想做什么(关于关系,
$parent
名称可能会产生误导)…我不知道