I don't think you can inherit from a class which name is not written in the PHP script.
Up vote 3 down vote favorite share g+ share fb share tw.
This is going to be strange. I have a database class for MySQL, and independent classes. For example: class users extends MySQL this class is a general class for users, so that it can be used more times.
But occasionally, an "MySQL_DEBUGGED" class is present, and if so, I want to extend from it: class users extends MySQL_DEBUGGED I want to inherit MySQL_DEBUGGED if present, otherwise MySQL class. If I put it into variable, it drops an error. How to do it?
Php class variables inherit link|improve this question asked Aug 13 '11 at 16:02deeped161.
It seems to me that the problem is that you have two classes instead of just one that can be configured to be more verbose (ie: to debug) or not – laurac Aug 13 '11 at 16:37 I don't know what are you doing but if you have two classes only because of debugging, you should be using a logger (ie: log4php) and configure it for your needs – laurac Aug 13 '11 at 17:00.
I don't think you can inherit from a class which name is not written in the PHP script. A possible solution would be : to define two classes, both called MySQL, in two separate files make your users class always extend the MySQL class (which means one of those two) Depending on the situation, include : the file that contains the "production" MySQL class, or the file that contains the "development" MySQL class This way, your class always extends a MySQL class -- but you'll include the right one. Basically, in your main file, you'd have : if (DEBUG) { require __DIR__ .
'/MySQL-debug. Php'; } else { require __DIR__ . '/MySQL-production.
Php'; } class users extends MySQL { // ... } And both MySQL-debug. Php and MySQL-production. Php will contain a class called MySQL -- which would not contain the same stuff in both files, of course.
1, This is a far better way to do it. – Brad Aug 13 '11 at 16:08 this looks better, but MySQL-debug inherits MySQL-production :) so class names cant be the same – deeped Aug 13 '11 at 16:12 1 @deeped humph, too bad ; and what about having both MySQL-debug and MySQL-production extend a common MySQL-common class, that would contain all the common code? – Pascal MARTIN Aug 13 '11 at 16:13.
All you need to do is use the class_exists() function. If (class_exists('MySQL_DEBUGGED')) { class users extends MySQL_DEBUGGED { .... } } else { class users extends MySQL { .... } } I wouldn't recommend it though. Conditionally declaring classes seems like a nightmare.
Thats right, but then all the methods must be copy/pasted, thats what I want to avoid – deeped Aug 13 '11 at 16:10.
The easiest solution would be to have an empty "bridge" class and inherit always from that one. Then, the only class that you would need to have declared twice would be the empty one. If (class_exists('MySQL_DEBUGGED')) { class MySQLBridge extends MySQL { } } else { class MySQLBridge extends MySQL_DEBUGGED { } } class User extends MySQLBridge { // ... your code ... } And finally on your pages: require_once('User.
Php'); $user = new User(); Other suggested solutions require you to have two copies of your inherited class, which I don't recommend.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.