Volte Posted June 20, 2007 Share Posted June 20, 2007 Hey guys! Wow. It has been forever. I got caught up in college (dropped out) and then started working for software companies. My first job was UI developer front end stuff for a small startup where I got into RoR (eeek!) and now my current job I'm back into PHP. A bit rusty, but I've bought out the PHP40 (WD40? ok sorry) Anyway. So I've got this problem, which only exists in my head as a possibility because of my experience with RoR. Here is what I'm gunning for. static class BaseClass extends CoreClass { static function BaseFunction($Param) { return "You Said $Param"; } ... } class MyClass extends BaseClass { ... } Alright, so what I want to happen, is for MyClass to extend BaseClass to the point where it inherits all the properties of BaseClass, but only those functions can be used as static on MyClass. So for instance; MyClass::BaseFunction(); Yet, when I say; $myclass = new MyClass(); I don't want $myclass (instance of MyClass) to be able to access BaseFunction(); So; $myclass->BaseFunction(); should thrown an exception, i.e. I would never call that because it is [should be] out of context. Does this make sense? I basically want a base class that I can define a bunch of methods upon, so that the child class (class that extends the base class) can only use those functions in a static sense (do they call that singleton?). Yet, to complicate things. I want MyClass to be instantiable (sp?). Wow, ok. I tried my best here. Let me know if certain parts need clarification. Hopefully I didn't leave behind too much carnage. Cheers! -- Volte Quote Link to comment Share on other sites More sharing options...
KrisNz Posted June 21, 2007 Share Posted June 21, 2007 php doesn't have static classes, only methods and properties. If you dont want baseclass to be instantiable, mark it as abstract. marking a method as abstract wont generate an error/warning if you call it like $myclass->BaseFunction(); But trying to call a static method, nonstatically(while it won't cause a fatal error) will generate a warning with error reporting set to E_STRICT. So yeah, what you want isn't really possible AFAIK. This may help.... http://au3.php.net/manual/en/language.oop5.php Quote Link to comment Share on other sites More sharing options...
Jenk Posted June 25, 2007 Share Posted June 25, 2007 have the constructor set to private, or if you are still using the stone age php4, trigger_error(). <?php class Foo { private function __construct () {} // prevents object instantiation } ?> 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.