PHP:调用另一个类的方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13503734/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:35:54  来源:igfitidea点击:

PHP: Calling another class' method

phpoop

提问by Ryan

I'm still learning OOP so this might not even be possible (although I would be surprised if so), I need some help calling another classes method.

我仍在学习 OOP,所以这甚至可能不可能(尽管如果是这样我会感到惊讶),我需要一些帮助来调用另一个类方法。

For example in ClassA Ihave this method:

例如在ClassA I有这个方法:

function getName()
{
    return $this->name;
}

now from ClassB(different file, but in the same directory), I want to call ClassA's getName(), how do I do that? I tried to just do an include()but that does not work.

现在从ClassB(不同的文件,但在同一目录中),我想调用ClassA's getName(),我该怎么做?我试图只做一个,include()但这不起作用。

Thanks!

谢谢!

回答by codingbiz

//file1.php
<?php

class ClassA
{
   private $name = 'John';

   function getName()
   {
     return $this->name;
   }   
}
?>

//file2.php
<?php
   include ("file1.php");

   class ClassB
   {

     function __construct()
     {
     }

     function callA()
     {
       $classA = new ClassA();
       $name = $classA->getName();
       echo $name;    //Prints John
     }
   }

   $classb = new ClassB();
   $classb->callA();
?>

回答by PeeHaa

If they are separate classes you can do something like the following:

如果它们是单独的类,您可以执行以下操作:

class A
{
    private $name;

    public function __construct()
    {
        $this->name = 'Some Name';
    }

    public function getName()
    {
        return $this->name;
    }
}

class B
{
    private $a;

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

    function getNameOfA()
    {
        return $this->a->getName();
    }
}

$a = new A();
$b = new B($a);

$b->getNameOfA();

What I have done in this example is first create a new instance of the Aclass. And after that I have created a new instance of the Bclass to which I pass the instance of Ainto the constructor. Now Bcan access all the public members of the Aclass using $this->a.

我在这个例子中所做的是首先创建一个A类的新实例。之后,我创建了一个B类的新实例,我将 的实例传递A给构造函数。现在B可以访问的所有公共成员A使用类$this->a

Also note that I don't instantiate the Aclass inside the Bclass because that would mean I tighly couple the two classes. This makes it hard to:

另请注意,我不会在A类中实例化B类,因为这意味着我将两个类紧密耦合。这使得很难:

  1. unit test your Bclass
  2. swap out the Aclass for another class
  1. 单元测试你的B课程
  2. A班级换成另一个班级

回答by Rawkode

You would need to have an instance of ClassA within ClassB or have ClassB inherit ClassA

您需要在 ClassB 中拥有 ClassA 的实例或让 ClassB 继承 ClassA

class ClassA {
    public function getName() {
      echo $this->name;
    }
}

class ClassB extends ClassA {
    public function getName() {
      parent::getName();
    }
}

Without inheritance or an instance method, you'd need ClassA to have a static method

没有继承或实例方法,你需要 ClassA 有一个静态方法

class ClassA {
  public static function getName() {
    echo "Rawkode";
  }
}

--- other file ---

--- 其他文件 ---

echo ClassA::getName();

echo ClassA::getName();

If you're just looking to call the method from an instance of the class:

如果您只是想从类的实例中调用该方法:

class ClassA {
  public function getName() {
    echo "Rawkode";
  }
}

--- other file ---

--- 其他文件 ---

$a = new ClassA();
echo $a->getName();

Regardless of the solution you choose, require 'ClassA.phpis needed.

无论您选择哪种解决方案,require 'ClassA.php都是需要的。

回答by martin-lundberg

File 1

文件 1

class ClassA {

    public $name = 'A';

    public function getName(){
        return $this->name;
    }

}

File 2

档案 2

include("file1.php");
class ClassB {

    public $name = 'B';

    public function getName(){
        return $this->name;
    }

    public function callA(){
        $a = new ClassA();
        return $a->getName();
    }

    public static function callAStatic(){
        $a = new ClassA();
        return $a->getName();
    }

}

$b = new ClassB();
echo $b->callA();
echo $b->getName();
echo ClassB::callAStatic();