PHP: Calling another class' method

后端 未结 4 1960
别跟我提以往
别跟我提以往 2020-12-23 09:50

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.

For example in

相关标签:
4条回答
  • 2020-12-23 10:18

    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 A class. And after that I have created a new instance of the B class to which I pass the instance of A into the constructor. Now B can access all the public members of the A class using $this->a.

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

    1. unit test your B class
    2. swap out the A class for another class
    0 讨论(0)
  • 2020-12-23 10:31

    File 1

    class ClassA {
    
        public $name = 'A';
    
        public function getName(){
            return $this->name;
        }
    
    }
    

    File 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();
    
    0 讨论(0)
  • 2020-12-23 10:35
    //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();
    ?>
    
    0 讨论(0)
  • 2020-12-23 10:38

    You would need to have an instance of ClassA within ClassB or have ClassB inherit 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

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

    --- other file ---

    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.php is needed.

    0 讨论(0)
提交回复
热议问题