chronister Posted December 2, 2008 Share Posted December 2, 2008 Hello All, I am taking a serious leap into OOP and am attempting to create a form class to make forms easier to deal with. I am trying to spread a class over multiple files. Basically I want to do something like this... main.class.file.php <?php class myClass{ var $myVar; var $messages; function myClass(){ $this->myVar = 1; include('path/to/file'); // contains methods I wish to be available in this class } function someMethod() { $this->addMessage('1','hello world'); // addMessage is defined in the included file echo $this->messages; } } ?> included file <?php class errorMessage extends myClass{ function addMessage($key,$message) { $messages[$key] = $message; $this->messages = $messages; return $this->messages; } } ?> I hope this makes sense, I am trying to create 1 class split into different files for easier code management, but have all methods and objects available anywhere after the include statement. Is this possible, or am I going about it in the wrong way?? Thanks, nate Quote Link to comment Share on other sites More sharing options...
trq Posted December 2, 2008 Share Posted December 2, 2008 Its doesn't exactly work that way. Firstly, from an OOP perspective errorMessage isn't really at all related to myClass so it defintaely shouldn't extend it. You would be better to have errorMessage as a completely seperate entity IMO. errorMessage.class.php <?php class errorMessage { private $messages; public function __construct() { $this->messgaes = array(); } public function addMessage($message) { $this->messages[] = $message; } public function getMessages() { return $this->messages; } } ?> <?php include 'errorMessage.class.php'; class myClass { private $myVar; private $messages; public function __construct() { $this->myVar = 1; $this->messages = new errorMessage; } function someMethod() { $this->messages->addMessage('hello world'); echo implode('<br />', $this->messages->getMessages()); } } ?> Quote Link to comment Share on other sites More sharing options...
chronister Posted December 2, 2008 Author Share Posted December 2, 2008 Ahhhh... Thank you thorpe. I kinda thought that I was going about it in the wrong way. This is the first big complete class I have tried writing. I have messed with modifying other classes, but not created one on my own. Thanks for this line, I did not realize I could access the object like this. $this->messages->addMessage(); Thanks, that will get me well on my way with this. Nate Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.