How to call the class?

Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
Hello! Help to understand how best to cause the desired class.
I will simplify the statement of the issue to a few modules on the site... for example, there are modules: review, posts, users.
Each module can have its own set of features (like comments and posts, subscribe to user, etc.). Take for example just like.
Huskies I have put on the principle: the object type and its ID is sent to a single file on the server like.php. So putting like to post 333, file like.php gets the value of a: type == post and id == 333

Next, for each type of object has its own classes, which are responsible for generating the notification object in the DB. ie Setting like post 333, file like.php should create an object of class LikePost and call the method createNotify passing the desired parameters. Here's how I do it now:
// this file like.php //......... switch($object_type){ // check object type and call a method of creating a notification of the desired class case 'post': /* * because the file is in like.php knows only the type and ID of the object * it is already inside a class is called LikePost model (in this case post) * and get the author ID of the post to write it together with the notice as * recipient of the notification */ LikePost::createNotification($auth->user_id, $object_id); break; case 'comment': // the same meaning when type == post LikeComment::createNotification($auth->user_id, $object_id); break; case 'news': // the same meaning when type == post LikeNews::createNotification($auth->user_id, $object_id); break; //..... etc. } //.........

Everything works, it works, but the code is very scattered, it turns out that if an object can subscribe, like, comment, share. In each such file as like.phpI need to build such tests. And when you add on the site of a new object type to remember to add all this for each such file, moreover, changing the name of the object type, I will need every tick of a file search for it and rename it.
As far as I know is not the right approach, but the other I can not think of, so please explain how actually beautiful it is?
by | 31 views

3 Answers

0 like 0 dislike
It is difficult to understand what you have there in General.

Wouldn't it be better to implement the desired interfaces directly on objects of type Post? That is running $post->like() and the post already runs the creation of the object, like with the necessary parameters. Get rid of a lot of a long switch when adding a new object type implements the interfaces directly in it.

Read patterns I think you will find the combination that will solve your problem.
0 like 0 dislike
For example:

$class_name = 'Like'.mb_strtoupper($object_type); $class_name::createNotification($auth->user_id, $object_id);

But be careful with the incoming data)
0 like 0 dislike
Need a white sheet of $object_type, to solve the security problem.
In principle, you have begun to do, only option is using switch-case bulky.
It is much better to make a mapping of the form
$classMap = [ 'some_input_obj_type' => RealClassName::class, 'another_input_obj_type' => RealAnotherClassName::class, ];

And then to use the design:
if (!isset($classMap[$object_type])) { //some code that return an error } $class = $classMap[$object_type]; $class::someMethod('some args1');

Related questions

0 like 0 dislike
1 answer
asked May 22, 2019 by romaaa32
0 like 0 dislike
2 answers
0 like 0 dislike
1 answer
asked Apr 14, 2019 by Farrien
0 like 0 dislike
7 answers
asked May 12, 2019 by zahardzhan
0 like 0 dislike
1 answer
asked Apr 21, 2019 by nen0y
110,608 questions
257,187 answers
40,796 users