2

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"?

3
  • 1
    What do you intend to do? Are you using PHP 4 or 5? If you're using PHP 5 you should declare your methods static if you intend to use them statically. Commented Dec 18, 2010 at 6:55
  • @BoltClock, Right... This is just an example. I will be using static or public for the methods. Commented Dec 18, 2010 at 6:59
  • Yeah it wasn't clear at first since the code example is valid PHP 4 (other than your method call). Commented Dec 18, 2010 at 7:00

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

Add () in the method call

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

OR

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

1 Comment

Thanks... I can't believe I missed them.
1
class firstclass
{
    public static function method1()
    {
       return "foo";
    }
}
class secondclass
{
    public function method2()
    {
        $myvar = firstclass::method1();
        return $myvar;
    }
}

Comments

0

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());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.