0

For example, I got the following classes: \App\User, \App\Profile and \App\Icon.
Every class got a static function creating which accepts a closure as paramter. Now I got an array of them:

$classes = [
   \App\User::class,
   \App\Profile::class,
   \App\Icon:class
];

Now I want to loop trough the array and call the creating function with a closure on every class. My problem is that I don't know much about the ::class keyword and how to use it proberly.

2 Answers 2

1

::class will just return the fully-qualified class name.

$classes = [
\App\User::class,
\App\Profile::class,
\App\Icon::class
];

Is the same (effectively) as:

$classes = [
   "\App\User",
   "\App\Profile",
   "App\Icon"
];

So it's iterable just like normal dynamic class names:

foreach($classes as $class){
   $class::creating($closure);
}
Sign up to request clarification or add additional context in comments.

Comments

0

Maybe I would operate with the class names. Without having tried:

$classes = [
   'User',
   'Profile',
   'Icon'
];
foreach ($classes as $class) {
   \App\$class::creating($closure);
}

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.