TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   Classes (Explained inside) (http://www.talkphp.com/advanced-php-programming/5046-classes-explained-inside.html)

Etheco 10-21-2009 07:39 PM

Classes (Explained inside)
 
Hi There as you can tell i am new to the community so firstly.

Hey all :D

Right to go into my problem. This is to write my own class base and core system. Read the code below to understand me further.

So all classes below will be seperate files. and will be included seperatly.

PHP Code:

class Base {
   public function 
load_class($class_name) { 
      include(
'core/'.$class_name.'.class.php');
      
$this->$class_name = new $class_name();
      }
}

class 
Logging {
   public function 
error($msg) {
      
print_r($msg);
      }

}

class 
Database extends Base {
   public function 
connect($coninfo) {
      
$this->Logging->error($coninfo);
      }

}

include(
'core/Base.class.php');
$Base = new Base();

$Base->load_class('Logging');
$Base->load_class('Database');
$Base->Database->connect('info'); 

Ok it all works up untill i run the connect, when i try to use $this->Logging->error(); it produces a error

Fatal error: Call to a member function error() on a non-object

Hope i explained it ok :(. Basicly i need to be able to use the classes i setup in the Base obj.

Thank you so so much in advanced :D

adamdecaf 10-21-2009 11:08 PM

1) I don't think you need to include Database, it should be 'auto-added' into the Base class.

2) Why not use separate variables for each class? (e.g. $database, $system, $validation...)

tony 10-21-2009 11:22 PM

In a hurry notice, I would say that it is because the error method has to have an argument to work. But the 'on a non-object' part tells me that the $Base->Database property is not an object instantiation.
Maybe changing this:
PHP Code:

$this->$class_name = new $class_name(); 

into this:
PHP Code:

$this->{$class_name} = new $class_name(); 

would help.

I don't remember very well the syntax rules in using variables as processing code text.

delayedinsanity 10-22-2009 06:05 AM

You'll probably need to a) load the logging class as an object of your database class, or b) declare the necessary properties as static and reference it via 'parent::' from your child class.

Your Database object, even though it extends the Base class, does not have access to the same properties that your Base object does. This is because they are essentially two seperate objects with their own set of properties - they're just sharing methods (physically they wouldn't share the same bubble, each is inside their own). You can visualize this by declaring some random properties in each, and then dumping $this with print_r() or var_dump().

This would probably be a great time to learn about design patterns. In particular, you might want to learn how to generate a singleton. Rather straight forward, and I believe Tony Marston has a very good write up on how to go about it on his site.
-m

Tanax 10-22-2009 10:04 AM

Database is not a variable nor function inside Base class so you cannot access it via $base->Database.

You would need a __get method and retrieve the object from an array of objects. And you also need to store the objects in that array when you use load_class.

sketchMedia 10-22-2009 10:35 AM

Tanax is correct, just because an class 'extends' another it doesn't mean that the 'base' class will have any knowledge of any derived objects. To add this functionality you would need to store the object in an array in the base object and use a getter to access it (as Tanax suggested you could use the magic get interceptor function __get)

Etheco 10-22-2009 01:45 PM

Thank you all so much for your replys, When i get a some spare time will put into place some the ideas you have provided. Wasn't expecting such a great response :D.

Liking this community already.


All times are GMT. The time now is 10:37 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0