Dynamic Class
Moderator: General Moderators
Dynamic Class
I was wondering if anyone knew how to dynamically create a class.
here is my idea. depending on what the class needs to do it'll open a file and assign functions to itself. doing this will hopefully reduce loading time since it won't load everything that can possible be done at once. Does this make sense??
here is my idea. depending on what the class needs to do it'll open a file and assign functions to itself. doing this will hopefully reduce loading time since it won't load everything that can possible be done at once. Does this make sense??
- raghavan20
- DevNet Resident
- Posts: 1451
- Joined: Sat Jun 11, 2005 6:57 am
- Location: London, UK
- Contact:
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Dynamic Class
You can dynamically create classes in PHP using eval(). If you want me to post some mixin code I'd be glad to.xcom923 wrote:I was wondering if anyone knew how to dynamically create a class.
Not quite following that, nor do I think this is a performance idea in general.xcom923 wrote:here is my idea. depending on what the class needs to do it'll open a file and assign functions to itself. doing this will hopefully reduce loading time since it won't load everything that can possible be done at once. Does this make sense??
(#10850)
OK this is what I mean. I usually get Queries from the URL. so lets say I say "?run=userCP" I'd want to have the class look in a file and register some functions to itself (being the class) I'm mainly doing this because it'll allow me to go though same classes without loading what I don't need.
the way I have to code is everything ultimately in one master class but loading an entire class for lets say I was doing a music system that adds URLs for music and displays a list of those same URLs as well as allows you to modify the URLs. I want to only load what the user is doing this way it'll save a little on loading time as well as server impact.
is that a little clearer??
the way I have to code is everything ultimately in one master class but loading an entire class for lets say I was doing a music system that adds URLs for music and displays a list of those same URLs as well as allows you to modify the URLs. I want to only load what the user is doing this way it'll save a little on loading time as well as server impact.
is that a little clearer??
I'm not sure I follow but I'll throw out an idea and see if it helps.
The query string tells you what folder/directory the class is stored in. But the class always has the same name. As long as the function does a similar thing, process form or something like that it might work out okay. I've seen this work out okay before.
The query string tells you what folder/directory the class is stored in. But the class always has the same name. As long as the function does a similar thing, process form or something like that it might work out okay. I've seen this work out okay before.
not quite what I'm talking about. The class loads and it has a switch inside the autoloader function that reads the query and executes a function based on what the query is. What I'm looking for is a way for it to load up a file and assign the function before actually running it. that way since usually there are lots of possiblities I can cut down on the file size
-
d3ad1ysp0rk
- Forum Donator
- Posts: 1661
- Joined: Mon Oct 20, 2003 8:31 pm
- Location: Maine, USA
Why not just use a basic MVC pattern?xcom923 wrote:not quite what I'm talking about. The class loads and it has a switch inside the autoloader function that reads the query and executes a function based on what the query is. What I'm looking for is a way for it to load up a file and assign the function before actually running it. that way since usually there are lots of possiblities I can cut down on the file size
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
I do something similar to dynamically make calls to modules and such
as mentioned erlier, in combination with __autoload()
Code: Select all
$moduleCall = new $controller;
$result = $moduleCall->{$requestMethod}();- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
xcom923, it sounds like you are talking about a Front Controller which centralize calling like that. It dispatches classes using the Command pattern. So your URL "?run=userCP" would load class named userCP, instansiate an object of that class, and then run $usercp->execute(). I think we can come up with some some examples of that.
(#10850)
I know there is a way to assign a function to a class outside its declaration (but I can't find how to do it). What I want to do is simular to that but only inside the object. IE if I created a function like thisJcart wrote:I do something similar to dynamically make calls to modules and such
as mentioned erlier, in combination with __autoload()Code: Select all
$moduleCall = new $controller; $result = $moduleCall->{$requestMethod}();
_______________
function foo(){
echo 'this is foo';
}
_______________
and the way to assign it inside a class call 'megaClass' is
_______________
$Obj = new megaClass();
$Obj->define_function('foo'); //I just made that the way to do it since I don't remember
_______________
what I'm trying to do is more like this
_______________
class megaClass{
function register($function = NULL){
if($function != NULL){
$This->define_function($function);
}
}
}
________________
does that help? the whole point is that I'll have the functions that would be assigned in a different file and include those files when I need them.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
I agree with feyd, you are trying to do something a certain way rather than understanding standard OOP methodology. For example with composition you can do something like this (PHP4 compatable code):You can do trickier things in PHP but they usually cause more problems than they solve.
Code: Select all
class myClass{
function myClass(&$function){
$this->myfunction = &$function;
}
function myFunction($arg){
$this->myfunction($arg);
}
}(#10850)
I understand standard OOP but I had an idea and just wasn't sure how to implement it. Now I'm caught on a different project but I still wanna try this out. the reason I'm doing this is mainly because I have to work with other code. the details are a little complicated but all in all arborint that snippet of code made me want to try something. I'll get back to you with the results
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
MVC has a number of connotations, but is often implemented with a Front Controller in PHP. In fact I think the Front Controller is probably the solution of choice for most enterprise type PHP coders. Here is an example:Gambler wrote:MVC requires you to load and parse code for all methods in a class. So if you have contoller with 40 methods, you're parsing 40 methods, while using only one. I think, that's what xcom923 is talking about.Why not just use a basic MVC pattern?
Code: Select all
<?php
class FrontController{
var $action_dir;
var $default_action;
var $error_action;
var $action_param;
var $action = '';
function FrontController($action_dir='action', $default_action='home', $error_action='error', $action_param='action'){
$this->action_dir = $action_dir;
$this->default_action = $default_action;
$this->error_action = $error_action;
$this->action_param = $action_param;
}
function & commandFactory($action){
$obj = null;
$filename = $this->action_dir . $action . '.php';
if (file_exists($filename)) {
include($filename);
if (class_exists($action)) {
$obj =& new $action();
}
}
return $obj;
}
function execute(){
if (isset($_GET[$this->action_param])) {
$this->action = preg_replace('/[^a-zZ-Z0-9\_\-]/', '', $_GET[$this->action_param]);
} else {
$this->action = $this->default_action;
}
$obj = & $this->commandFactory($this->action);
if (! $obj) {
$obj = & $this->commandFactory($this->error_action);
}
$obj->execute();
}
}
?>A Front Controller accepts URL in to form "www.mysite.com/controller.php?action=home" or if it in index.php just "www.mysite.com/?action=home" or with clean URLs it might be "www.mysite.com/home/".
Here are the other files (if you dump them all in a directory you should be able to see them work by passing various "?action=" params like 'home', 'page' or ''):
index.php (or some other name like controller.php)
Code: Select all
<?php
include 'FrontController.php';
$fc =new FrontController('./', 'home', 'error');
$fc->execute();
?>Code: Select all
<?php
class home{
function execute(){
echo 'Home';
}
}
?>Code: Select all
<?php
class error{
function execute(){
echo 'Error';
}
}
?>Code: Select all
<?php
class page{
function execute(){
echo 'Page';
}
}
?>This code really should be expanded with a little better error handling, maybe plugabble Action Factories, Request and Response objects, etc. And adding a Service Locator object, that gets passed everywhere, to tie it all together would make it the basis of a complete framework.
(#10850)