1

Have a Class

class SomeClass1{
    public static function myStatic($arg){
        return (new SomeClass1)->myMethod($arg);
    }

    public function myMethod($arg){
        return $arg;
    }
}

var_dump((SomeClass1::myStatic('123')));

I really do not like this part

(new SomeClass1)->myMethod($arg);

Are there any other ways?

Update Also I can call it like

(new static)->myMethod($arg);
3
  • 1
    No since you need an instance of the object to access normal methods ?! Commented Apr 9, 2015 at 17:14
  • 1
    You usually create static functions in a class to be used as helpers, if you're trying to access class members inside a static function there must be a better way to achieve what you want. Commented Apr 9, 2015 at 17:14
  • If the method does not depend in any way on an object or its properties, it should probably be static if it should be there at all... Commented Apr 9, 2015 at 17:38

3 Answers 3

1

If you have to do that, it is probably an indicator that that method should be static. If you want to call a non-static function, you need an instance of the object.

That being said, there are many options to get an object. Perhaps you make a new, temporary one (as you example looks like). Perhaps there is a static class variable that is an instance of the class for doing things like this.

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

Comments

0

If myMethod($arg) does not require the state of an instantiated object to return (and it doesn't, based on your example code), then you should just make that method static, and call it with var_dump( SomeClass1::myMethod('123') );

If you're doing this because you also need to call the method from within the object when it is instantiated, you can use self::myMethod('123') from within another method.

Comments

0

The short answer is no.

The long answer is no, and you should avoid writing static methods altogether. Static methods making testing harder. You may not be at the point where you are testing your code (you should start immediately) but the least you can do to help your self right now is to stop writing static method. Hard Stop. No exceptions.

I realize creating an instance just to use a method that has no state seems like extra typing, but in the long run it will pay off.

Some support for why static methods suck:

http://googletesting.blogspot.com/2008/12/static-methods-are-death-to-testability.html

http://www.giorgiosironi.com/2009/11/mocking-static-methods-road-to-hell.html

It's not testing the method itself that causes the problem, its when you are testing other classes and methods that rely on other static methods--forcing you to usually set up a ton of context to get all the other static things to work so you can test the current method you care about. It is much easier to mock your dependencies so you can test the subject directly.

If you need a resource to get into testing, heres one (there are many!). http://www.sitepoint.com/tutorial-introduction-to-unit-testing-in-php-with-phpunit/

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.