Skip to content

Tag: php

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…

DateTime with microseconds

In my code, I’m using DateTime objects to manipulate dates, then convert them to timestamp in order to save them in some JSON files. For some reasons, I want to have the same thing as DateTime (or something close), but with microseconds precision (that I would convert to float when inserting inside the …

Check if one of multiple words exists in the string?

I have a string like this: I want to check it for these words: it, test. I want to it returns true if there is at least one of those words in the string. Here is what I did: (though it does not work) How can I do that? Answer Simply checking using preg_match(), you can add many different words

Clone entity and all related entities in CakePHP 3

In my CakePHP 3 app, I have a somewhat elaborate tree of entities that I need to clone and save. The root of the structure is a Questionnaire, a Questionnaire hasMany Questions, each Question hasMany Fields, etc. (it goes deeper). Now I want the user to be able to define a new questionnaire by copying an old …