Jump to content

static classes?


Volte

Recommended Posts

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) :D

 

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.