Calling class names encapsulated as strings

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
scarabatwork
Forum Newbie
Posts: 6
Joined: Sat Mar 01, 2003 5:34 pm
Location: Dallas, TX
Contact:

Calling class names encapsulated as strings

Post by scarabatwork »

JayBird | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I have a content management system that I've been working on for the last few weeks and have run into an issue that I can't seem to figure out.  What I would like is to be able to use classes as "modules" that could be activated or deactivated.  Those classes would be listed in database form and then called in series.  The problem that I've run into is that I can't seem to find anything other than call_user_func as a way of instantiating the objects.  Here is an exmple of what I would like to accomplish:

Code: Select all

<?php

class TestClass
{
  private $property;
  function __constructor()
  {
      $this->property = "Some value";
  }

  function SomeMethod($inputVar)
  {
      return $inputVar++;
  }
}

$className = "TestClass";

//code that wont work...
$obj = new $className();

?>
I can use:

Code: Select all

$call = array("TestClass","SomeMethod");
echo call_user_func($call,3);
As long as there aren't any calls to the class from within the method such as $this->SomeMethod() this works fine, otherwise I get an error message:

Code: Select all

Using $this when not in object context
If anyone has a solution to this issue, please let me know.


JayBird | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

I do something very similar to this (using 'dynamic classes').

I think what you have should work you just need to remove the parens.

try something like this:

Code: Select all

<?php

class TestClass
{
  private $property;
  function __constructor()
  {
      $this->property = "Some value";
  }

  function SomeMethod($inputVar)
  {
      $inputVar++;
	  return $inputVar;
  }
}

$className = "TestClass";

//code that wont work...
$obj = new $className;
echo $obj->SomeMethod(3);

?>
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

That code should work with the parens. The reason you get an error with call_user_func() is that you are calling the class statically -- not calling a method in an instance. What version of PHP are you using? Can you give us the exact error message or what "won't work" means?
(#10850)
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

You can use reflection as well:

Code: Select all

<?php

$reflect = new ReflectionClass('SomeClass');

$obj = $reflect->newInstance();

$obj->SomeMethod('foo');

?>
You can pass arguments to it as well. http://php.net/reflection
Post Reply