Page 1 of 1

Calling class names encapsulated as strings

Posted: Tue Oct 03, 2006 11:23 pm
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]

Posted: Tue Oct 03, 2006 11:44 pm
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);

?>

Posted: Tue Oct 03, 2006 11:52 pm
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?

Posted: Wed Oct 04, 2006 4:14 am
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