Jason
Jason

Reputation: 2727

php OOP call to a method in a class outside the current class

Is there a way to call a method in a class outside of the current class? For an example:

class firstclass
{
    function method1()
    {
       return "foo";
    }
}
class secondclass
{
    function method2()
    {
        $myvar = firstclass::method1;
        return $myvar;
    }
}

Where the $myvar = firstclass::method1; is an attempt to access the firstclass method1.
The desired result in this example would be $myvar = "foo".

I know I could use "extend", but wanted to know if there was a way to do it without "extend"?

Upvotes: 2

Views: 5184

Answers (4)

John Smith
John Smith

Reputation: 1110

Your source code is not a good example of object oriented programming at all.

You SHOULD always pass firstclass into a secondclass using for example constructor. Then you can call first class from inside the secondclass.

Object injection

class firstclass
{
    function method1()
    {
        return "foo";
    }
}

class secondclass
{
    /**
     * @var firstclass
     */
    private $firstClass;

    /**
     * @param firstclass $firstClass
     */
    public function __construct(firstclass $firstClass)
    {
        $this->firstClass = $firstClass;
    }

    function method2()
    {
        return $this->firstClass->method1();
    }
}

print_r((new secondclass(new firstclass))->method2());

Callable injection

If you don't want use the whole instance of firstclass inject callable inside the secondclass and then call it.

Calling method instance

class firstclass
{
    function method1()
    {
        return "foo";
    }
}

class secondclass
{
    /**
     * @var callable
     */
    private $method1;

    /**
     * @param callable $method1
     */
    public function __construct(callable $method1)
    {
        $this->method1 = $method1;
    }

    function method2()
    {
        return call_user_func($this->method1);
    }
}

print_r((new secondclass(array(new firstclass, 'method1')))->method2());

Calling static method

class firstclass
{
    static function method1()
    {
        return "foo";
    }
}

print_r((new secondclass('firstclass::method1'))->method2());

Upvotes: 0

matthew90
matthew90

Reputation: 11

class firstclass
{
    public static function method1()
    {
       return "foo";
    }
}
class secondclass
{
    public function method2()
    {
        $myvar = firstclass::method1();
        return $myvar;
    }
}

Upvotes: 1

Shakti Singh
Shakti Singh

Reputation: 86476

Add () in the method call

function method2()
    {
        $myvar = firstclass::method1();
        return $myvar;
    }

OR

function method2()
        {
            $firstObj= new firstclass();
            $myvar = $firstObj->method1();
            return $myvar;
        }

Upvotes: 1

BoltClock
BoltClock

Reputation: 724472

Assuming method1 is a static method, you just need parentheses. It's just like a normal function call.

$myvar = firstclass::method1();

Incidentally, if you don't do anything else with $myvar other than returning it, you can shorten your method into one line of code:

return firstclass::method1();

The purpose of the extends keyword is for when you want secondclass to inherit the properties and methods of firstclass. In other words, you establish a parent-child relationship between the two classes. If they're not related at all, then you should not declare secondclass extends firstclass. You can still make use of either class in the scope of other classes by simply referencing their class names.

Upvotes: 3

Related Questions