TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 10-21-2009, 07:39 PM   #1 (permalink)
The Visitor
 
Etheco's Avatar
 
Join Date: Oct 2009
Posts: 4
Thanks: 0
Etheco is on a distinguished road
Help 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
Etheco is offline  
Reply With Quote
Old 10-21-2009, 11:08 PM   #2 (permalink)
The Addict
 
Join Date: May 2009
Posts: 287
Thanks: 5
adamdecaf is on a distinguished road
Default

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...)
__________________
My Site
adamdecaf is offline  
Reply With Quote
Old 10-21-2009, 11:22 PM   #3 (permalink)
The Addict
 
tony's Avatar
 
Join Date: Aug 2008
Posts: 336
Thanks: 8
tony is on a distinguished road
Default

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.
tony is offline  
Reply With Quote
Old 10-22-2009, 06:05 AM   #4 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

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
delayedinsanity is offline  
Reply With Quote
Old 10-22-2009, 10:04 AM   #5 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

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.
__________________
Tanax is offline  
Reply With Quote
Old 10-22-2009, 10:35 AM   #6 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

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)
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Old 10-22-2009, 01:45 PM   #7 (permalink)
The Visitor
 
Etheco's Avatar
 
Join Date: Oct 2009
Posts: 4
Thanks: 0
Etheco is on a distinguished road
Default

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.
Etheco is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Abstract Classes sketchMedia Advanced PHP Programming 18 02-28-2013 05:38 AM
HTML Inside the class? Sirupsen General 16 06-06-2009 10:03 AM
[Tutorial] How to organize your classes | Part 1 Tanax Advanced PHP Programming 10 03-01-2009 10:08 PM
no "include"s inside classes? pipesportugal Absolute Beginners 8 06-08-2008 04:58 PM
PHP5 Classes A to Z Part 1 quantumkangaroo Advanced PHP Programming 11 04-01-2008 04:21 AM


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

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design