Skip to content

Tag: oop

When should I use static methods?

I have a class that is containing 10 methods. I always need to use one of those methods. Now I want to know, which approach is better? OR Answer It is an interesting subject. I’m gonna give you a design oriented answer. In my opinion, you should never use a static class/function in a good OOP architectu…

How to use class methods as callbacks

I have a class with methods that I want to use as callbacks. How can I pass them as arguments? Answer Check the callable manual to see all the different ways to pass a function as a callback. I copied that manual here and added some examples of each approach based on your scenario. Callable A PHP function is …

call methods and objects using php

i have this code : in my code i don’t always want to use $test = new test …. I want to use the name directly instead of $test. For example: Answer You should use a factory method pattern or something like that. With a factory method you can create a class based on a string. In PHP it’s not

when it is required that a class will be defined before instantiation?

In the PHP documentation it says: Classes should be defined before instantiation (and in some cases this is a requirement). can some one give me a example of a class that can not be instantiated unless it was previously defined? Answer The parsing rules are the same as for functions: if they’re defined …