DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Inheritance in PHP: A Simple Guide With Examples
  • The Blue Elephant in the Room: Why PHP Should Not Be Ignored Now or Ever
  • Laravel for Beginners: An Overview of the PHP Framework
  • Is PHP Still the Best Language in 2024?

Trending

  • A Guide to Developing Large Language Models Part 1: Pretraining
  • It’s Not About Control — It’s About Collaboration Between Architecture and Security
  • While Performing Dependency Selection, I Avoid the Loss Of Sleep From Node.js Libraries' Dangers
  • Mastering Fluent Bit: Installing and Configuring Fluent Bit on Kubernetes (Part 3)
  1. DZone
  2. Coding
  3. Languages
  4. Dynamic Class Instantiation and NS in PHP

Dynamic Class Instantiation and NS in PHP

In this article, a senior developer explains the use of dynamic class instantiation and namespacing in PHP, and some issues than can arise while using these methods.

By 
Behrad Khodayar user avatar
Behrad Khodayar
·
Apr. 30, 18 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
27.8K Views

Join the DZone community and get the full member experience.

Join For Free

The first step in developing every program’s business, right after designing and defining the required classes, is class instantiation. And it is a very straightforward process. You call the class constructor which is exactly the same as the class name, followed by the respective namespace, pass the required arguments (if any), and that’s it.

The scenario described above is exactly what happens in nearly all situations. But there is a condition where exceptions may arise: whenever you try to instantiate a class dynamically.

So, what is dynamic class instantiation/loading?

Dynamic Class Instantiation allows the loading of code that is not known about before a program starts. This way, the program doesn’t need to be aware of the class name before it's loaded and run.

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();

As you see, we first specify the desired class name in the flow of the program and then use it to declare the class. 

So far, what we’ve discussed is occurring in a global scope. Now its time to dig into Dynamic Class Loading in Namespaced PHP.

It seems there is nothing new to worry about: we’ll just declare some namespaces, then every package and class has to be defined in its proper namespace. So we just have to follow the rules about normal class loading. Let's do it and see what happens:

/* File Component/Package/Vehicle/BicycleModel.php */

namespace Component\Package\Vehicle;

class BicycleModel{
  //@ToDo: implement BicycleModel
}
------------------------------------------------------

/* File Component/Package/Vehicle/CarModel.php */

namespace Component\Package\Vehicle;

class CarModel{
  //@ToDo: implement CarModel
}
------------------------------------------------------

/* File Component/Package/Vehicle/VehicleFactory.php */

namespace Component\Package\Vehicle;

class VehicleFactory
{
  private $vehicleName;

  public function __construct($vehicle = 'Bicycle'){
    $this->$vehicleName = $vehicle;
  }

  public function getInstance(){
    $className = $this->$vehicleName . 'Model';
    return new $className(); //This will return \BicycleModel & NOT \Component\Package\Vehicle\BicycleModel
    /* This means it won't work as expected. We thought it will return a BicycleModel in the same namespace
    *  that is defined, But it didn't happen. Continue reading to see why */
  }
}

Defining namespaces and importing packages from other namespaces is performed at compile-time and so does not affect dynamic class names (which are determined at run-time).

So, in order to fix bug, we have to rewrite the  getInstance() method, prefixing te $className  with the proper namespace:

public function getInstance(){
  $className = 'Component\\Package\\Vehicle\\' . $this->$vehicleName . 'Model';
  return new $className(); //This will return \BicycleModel & NOT \Component\Package\Vehicle\BicycleModel
  /* This means it won't work as expected. We thought it will return a BicycleModel in the same namespace
  *  that is defined, But it didn't happen. Continue reading to see why */
}
PHP

Published at DZone with permission of Behrad Khodayar. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Inheritance in PHP: A Simple Guide With Examples
  • The Blue Elephant in the Room: Why PHP Should Not Be Ignored Now or Ever
  • Laravel for Beginners: An Overview of the PHP Framework
  • Is PHP Still the Best Language in 2024?

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!