Skip to content

Instantly share code, notes, and snippets.

@behkod
Last active April 26, 2018 09:33
Show Gist options
  • Save behkod/f410bfeb92d194f8d84b51b1d653080e to your computer and use it in GitHub Desktop.
Save behkod/f410bfeb92d194f8d84b51b1d653080e to your computer and use it in GitHub Desktop.
Normal vs. Dynamic Class Loading PHP
<?php
class BicycleModel{
//@ToDo: implement BicycleModel
}
class CarModel{
//@ToDo: implement CarModel
}
// Normal Class Loading
$myTodayChoice = new BicycleModel();
// Dynamic Class Loading
$vehicleName = 'Bicycle';
// You should first concatenate $vehicleName & Model, then use resulting variable for creating class instance.
// Keep in mind that you're not allowed to merge class instantiation & concatenation.
// $myTodayChoice - new $vehicleName.'Model'(); IS NOT VALID
$className = $vehicleName . 'Model';
$myTodayChoice = new $className();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment