Home > Software engineering >  PHP Calling A Class function inside B Class
PHP Calling A Class function inside B Class

Time:11-27

I Want to access a function outside of the class in another file, another class , thanks for all help!

file "a" class "a" function "a" --> file "b" class "b" function "b" (I want to use function "a" in this)

//file : a.php
class A 
{
public function a($string)
   {
     $string .= $string; // Not static 
     return $string;
   }

}
//file : b.php
require_once('a.php');


class B
{
public function b($string)
   {
     //here I need function "a"
     $string = a($string);
     return $string;
   }

}



CodePudding user response:

File a.php:

<?php
class A 
{
    public function a($string='')
    {
        $string .= $string; // Not static 
        return $string;
    }
}

File b.php:

<?php
require_once('a.php');

class B
{
    public function b($string='')
    {
        $aObj = new A();
        
        //here I need function "a"
        $string = $aObj->a($string);
        return $string;
    }

}
  • Related