pornophobic Posted September 4, 2010 Share Posted September 4, 2010 Not sure how to describe what I'm trying to do here in the title, but here goes with what I am trying to accomplish. I've got a few hundred lines of code in total so far, so I'll try to keep it as short as I can. I've got an application that I am programming using classes for each module and right now I am coding the base classes that I need in order for it to run (database, errors, logging, etc). What I'm doing for my database class is I have a query factory and it extends the MySQLi class so I can process, clean and code the rest of my app faster. I also have another, unrelated class "Error", which will be used for processing errors I might come across. I'd rather do it this way instead of having to call trigger_error and error_log every time there is an error. I'd also not like to have to call a new instance of an object every time I need to use something from that class. Is there any way I can call a class within a class and return it as an object for all the methods within the class? I've tried the methods below, but no luck I've tried others, but I'm trying to keep it brief and get what I'm trying to do across. <?php class QueryFactory extends MySQLi { public $err = new error(); //Doesn't work. public $err = error(); //Nope. #This is the function that I need the $err object for. function set($fields, $newvals) { if ( is_array($fields) && is_array($newvals) ) { if ( count($fields) != count($newvals) ) { //Instead of below, I want to do something like $err->('Array lengths must match for method', 256, $islogged = 1); trigger_error('Array lengths must match for method', 256); } } } } The thing is, I have a "run.inc.php" which does include and create new objects for running just the basic app and if I try to redeclare the error class in query.class.php, it gives me an error saying I can't do that, but if i try to call $err from the page that has all the classes defined it throws an error saying that my method is undeclared. I'd like my error class be available to every other class I create so I can display and log errors as needed. Any suggestions or links to point me where I'd like to go? Quote Link to comment https://forums.phpfreaks.com/topic/212485-unrelated-classes-within-classes/ Share on other sites More sharing options...
ignace Posted September 4, 2010 Share Posted September 4, 2010 You want to use Exceptions. Quote Link to comment https://forums.phpfreaks.com/topic/212485-unrelated-classes-within-classes/#findComment-1107128 Share on other sites More sharing options...
pornophobic Posted September 4, 2010 Author Share Posted September 4, 2010 Wonderful! That is EXACTLY what I was looking for! Thank you. Edit: I'm going to put some code so it might help someone in the future. query.class.php: <?php include 'error.class.php'; class QueryFactory extends MySQLi { /*somefunctions*/ #This is the finished function from the original that i used in the first post. # function set($fields, $newvals) { if ( is_array($fields) && is_array($newvals) ) { if ( count(array_diff_key($fields, $newvals)) !== 0 ) { try { throw new Error('Arrays must be same length.'); //Error is declared on error.class.php } catch(Error $e) { echo $e->showError(); } } } } } error.class.php: <?php class Error extends Exception { function showError() { $message = self::getMessage(); $line = self::getLine(); $file = self::getFile(); return 'Error: ' . $message . ' <br />In file: ' . $file . ':'. $line; //Above outputs something like //Error: Some error message //In file: some_file.php:146 } } Quote Link to comment https://forums.phpfreaks.com/topic/212485-unrelated-classes-within-classes/#findComment-1107155 Share on other sites More sharing options...
ignace Posted September 4, 2010 Share Posted September 4, 2010 It may also be a good idea to use a framework so you won't have to bother with writing all this tedious tasks and focus on writing the actual application. Quote Link to comment https://forums.phpfreaks.com/topic/212485-unrelated-classes-within-classes/#findComment-1107164 Share on other sites More sharing options...
pornophobic Posted September 4, 2010 Author Share Posted September 4, 2010 Not in my case, I need to know the code from scratch and pretty much build my own framework suited to it's needs. Plus, I've heard that frameworks are no good if you want flexibility. I mean, for the hobbyist they may be great, but that's not what I'm doing. I want and need to know as much as I can about PHP, and pretty much anything else I can get my eyes on. Not turning down your idea, though. This project won't even be using pre-made javascript libraries or templating engines. It is being documented well, though. For my own sake and the sake of any possible future coders. Quote Link to comment https://forums.phpfreaks.com/topic/212485-unrelated-classes-within-classes/#findComment-1107167 Share on other sites More sharing options...
ignace Posted September 4, 2010 Share Posted September 4, 2010 Plus, I've heard that frameworks are no good if you want flexibility. That depends on the kind of framework you chose for. A full-stack framework will limit you, a glue framework will not. I mean, for the hobbyist they may be great, but that's not what I'm doing. For the hobbyist? Frameworks like those of Zend are not merely for the hobbyist, if a hobbyist is even capable of getting past the steep learning curve. I want and need to know as much as I can about PHP Writing your own framework and using it's component will make you competent in PHP programming not in it's inner workings. If you want to learn what makes PHP tick you should start with the manual (especially the sections on Language Reference, PHP at the core, and the Appendices). Get your hands dirty, learn C and write a PHP extension. It is being documented well, though. For my own sake and the sake of any possible future coders. Documentation means nothing if the framework isn't properly tested. Quote Link to comment https://forums.phpfreaks.com/topic/212485-unrelated-classes-within-classes/#findComment-1107221 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.