Initialize a sub-class in a class
07-12-2011, 01:04 PM
|
Initialize a sub-class in a class
|
Posts: 20
|
Hey, I have a problem. I know the problem is really simple, I don't know to solve it.
OK, I write a simple PHP engine. I have a main class, called engine_class.php in the root, and sub classes in a subfolder called plugins. I've already wrote a autoloader etc, but here comes the problem.
The "engine_class.php":
PHP Code:
// Autoloader ..
class engine {
// Declarations, functions, etc...
function __call ($name, $args) {
$this->$name = new $name;
return call_user_func_array(array($this->$name, $name), $args);
}
}
(I've already created some sub-classes... cookie, mysql...)
Now i want, in example (index.php):
PHP Code:
include "engine_class.php";
$engine = new engine;
// static
$engine->cookie()::set("name", "value"); // I know that this don't work
// or non-static
$engine->mysql("localhost", "test", "test", "test");
$engine->mysql()->query("SELECT * FROM test");
Now, how can I do, that my sub-classes wil work like in my upper example in/with the main class?
So, that i can use it like this:
PHP Code:
$engine = new engine;
$engine->[subclass]()->[submethod]($arg1, $arg2, ...);
// or for static:
$engine->[subclass]()::[submethod]($arg1, $arg2, ...);
|
|
|
|
07-12-2011, 03:12 PM
|
Re: Initialize a sub-class in a class
|
Posts: 57
Name: Sachin Gutte
|
PHP Code:
// Autoloader .. // now following is code for entire engine.php class engine {
// Declarations, functions, etc...
protected function __call ($name, $args) {
$this->$name = new $name;
return call_user_func_array(array($this->$name, $name), $args);
}
}
class cookie extends engine{
// here you can access engine methods with like -- // parent::function_name($para);
protected function set($str,$value){ //set cookie here }
} class mysql extends engine{ function __construct($host,$username,$password){ //remaining code } //you can access engine methods with like -- // parent::function_name($para);
protected function query($str){ //execute the query }
}
then simply incude engine.php in your index.php file as-
PHP Code:
include 'engine.php';
$cookie=new cookie; $cookie->set('name','value'); $mysql=new mysql('localhost','user','password'); $mysql->query('selct *from table');
P.S. someone correct if i'm wrong here .
Last edited by phazorRise; 07-12-2011 at 03:14 PM..
|
|
|
|
07-12-2011, 04:12 PM
|
Re: Initialize a sub-class in a class
|
Posts: 20
|
Yeah, I know this style, but I don't want to create 100 new variables for database, cookie, mail,.. and other subclasses.I want to create a object, which includes all of it.
|
|
|
|
07-12-2011, 07:27 PM
|
Re: Initialize a sub-class in a class
|
Posts: 807
Name: Mattias Nordahl
Location: Sweden
|
When a plugin is loaded, create an instance of it's class and assign it to a variable with the class' name. I'm not entirely sure of the syntax here, but something along this line
PHP Code:
// your plugin loader function function load($class_name, $params) { $this->{strtolower($class_name)} = new {$class_name}($params); }
So, if you i.e. load the MySQL class, then you should be able to use the plugin object like so
PHP Code:
$engine->mysql->query('...');
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
|
|
|
|
07-12-2011, 10:22 PM
|
Re: Initialize a sub-class in a class
|
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
|
Here is a tip I've been using myself for an application I'm building. Using the overloading features are very handy. This will keep an array of objaect instances, so it will make the classes become singleton. You example was trying to intiate the class twice.
PHP Code:
class engine { protected static $_instances = array(); public function __call($className, $arguments) { if (isset(self::$_instances[$className])) return self::$_instances[$className]; if (!class_exists($className)) throw new Exception("Class $className does not exist"); self::$_instances[$className] = call_user_function(array($className, '__construct'), $arguments); return self::$_instances[$className]; } }
__________________
<mgraphic /> - I don't have a solution but I admire the problem.
|
|
|
|
07-13-2011, 06:12 AM
|
Re: Initialize a sub-class in a class
|
Posts: 20
|
Mgraphic, I used your code and when i call it like:
PHP Code:
engine::cookie()->set("name", "value"); // or engine::mysql("localhost", "test", "test", "test");
throws me error:
Code:
Call to undefined method engine::cookie()
// or
Call to undefined method engine::mysql()
and when i call it like:
PHP Code:
$engine = new engine;
$engine->cookie()->set("name", "value"); // or $engine->mysql("localhost", "test", "test", "test");
it throws me:
Code:
call_user_func_array() expects parameter 1 to be a valid callback, non-static method cookie::cookie() cannot be called statically in ...
// and
Call to a member function set() on a non-object in ...
// or this in the mysql class
call_user_func_array() expects parameter 1 to be a valid callback, non-static method mysql::mysql() cannot be called statically in
Last edited by hellboy124; 07-13-2011 at 07:06 AM..
|
|
|
|
07-13-2011, 07:23 AM
|
Re: Initialize a sub-class in a class
|
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
|
If you want the overload __call() function to be called staticly, you can rename it or copy it as __callStatic().
Change the callback argument of call_user_func_array() to $className instead of array($className, '__construct')
__________________
<mgraphic /> - I don't have a solution but I admire the problem.
|
|
|
|
07-13-2011, 10:14 AM
|
Re: Initialize a sub-class in a class
|
Posts: 20
|
I've try everything, but it dont work's :S
engine call, like you said:
PHP Code:
public function __call($className, $args) {
if (isset(self::$_instances[$className])) return self::$_instances[$className];
if (!class_exists($className)) throw new Exception("Class $className does not exist");
self::$_instances[$className] = call_user_func_array($className, $args);
return self::$_instances[$className];
}
cookie_class..
PHP Code:
class cookie {
// other funkcions....
public function set($name, $value, $expiry = 31536000, $path = '/') {
if ( !headers_sent() ) {
$expiry += time();
@setcookie($name, $value, $expiry, $path);
$_COOKIE[$name] = $value;
}
}
}
and calling of it:
PHP Code:
$engine = new engine;
$engine->cookie->set("name", "value");
and it throw me this errors...
Code:
Undefined property: engine::$cookie in...
//and
Call to a member function set() on a non-object in...
but if i try:
PHP Code:
$engine->cookie()->set("name", "value");
then:
Code:
call_user_func_array() expects parameter 1 to be a valid callback, function 'cookie' not found or invalid function name in...
//and
Call to a member function set() on a non-object in ...
 
|
|
|
|
07-13-2011, 10:19 AM
|
Re: Initialize a sub-class in a class
|
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
|
Take a look into PHP ReflectionClass - You can intiate a new class using
ReflectionClass::newInstance() and ReflectionClass::newInstanceArgs()
http://php.net/manual/en/class.reflectionclass.php
__________________
<mgraphic /> - I don't have a solution but I admire the problem.
|
|
|
|
07-13-2011, 10:35 AM
|
Re: Initialize a sub-class in a class
|
Posts: 20
|
Now it works
Thanks!! 
|
|
|
|
|
« Reply to Initialize a sub-class in a class
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|