// Assume there is a Category class with a constructor that takes a name parameter $className = 'Category'; $categoryName = 'Books'; // Create new instance of Category with name 'Books' $category = new $className($categoryName);
// Assume there are three classes that inherit from Category: BookCategory, MusicCategory, and MovieCategory $className = 'BookCategory'; $categoryName = 'Fiction'; // Create new instance of BookCategory with name 'Fiction' $category = new $className($categoryName);The Category newInstance method is part of PHP's Reflection package library. Reflection is a set of classes and functions that enable you to inspect and manipulate code at runtime. The Category class in the above examples does not necessarily belong to any package library.