• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

My Monkey Do

A Log of Coding Solutions

  • Home
  • Web Hosts
  • Tools
  • About

Creating an Instance of a PHP Class

May 15, 2011 by Webhead

Problem:

This works in PHP 5.3+:

$CLASS_NAME = 'TheName';

$class_instance = new $CLASS_NAME;

This doesn’t

define('CLASS_NAME', 'TheName');

$class_instance = new CLASS_NAME;

 

Workaround:

Use the way that works.  There are other limitations with using a variable to create a class.  Another way to create a class from a variable is by using:

$test = call_user_func(array($className, 'getInstance'));

 

Filed Under: Coding Tagged With: php

Primary Sidebar

Topics

apache apple block editor chrome cms css debug eCommerce embed firebug firefox git gmail goDaddy google hosting htaccess html html 5 IE crap image iPad iPhone javascript jquery linux localization mac os x ms sql mysql open source optimize php php 5.3 responsive rest api seo svg tinymce woocommerce wordpress wpengine xss yii youtube




Categories

  • Coding
  • Off the Shelf
  • Plugins
  • Random Thoughts
  • Server Stuff
  • Tools