Skip to content
Advertisement

Tag: oop

Serialize/deserialize nested objects to JSON with type in PHP

I have classes that extends an abstract class. I need to create instances of these classes through a string – preferably JSON. Many of the objects are nested, and many properties are private. I need a way to: Create a JSON string of the complete object (with private properties and nested objects – with their private properties). Create a new

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 architecture. When you use

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 passed

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

php – check if class name stored in a string is implementing an interface

I understand that my question is somehow wrong, but I’m still trying to solve this problem. I have an interface Programmer: and a couple of namespaced classes: StudentsBjarneProgrammer (implements Programmer) StudentsCharlieActor (implements Actor) I have this class names stored in array $students = array(“BjarneProgrammer”, “CharlieActor”); I want to write a function, that will return an instance of class if it’s

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 in the “top level” of a file,

Advertisement