Jump to content

PHP Class sharing?


jamesmpollard

Recommended Posts

Hi Guy's

 

Now this may be a stupid question but I was wondering if anyone could help me accomplish something. Here's what I'm trying to do (in brief).

 

CLASSONE.PHP

 

class one extends Core {
 
public function __construct() {
 
}
 
public function atestone() {
return $this;
}
 
public function __destruct() {
 
}
}

 

CLASSTWO.PHP

 

class two extends Core {
 
public function __construct() {
 
}
 
public function atesttwo() {
return $this;
}
 
public function __destruct() {
 
}
}

 

CORE.PHP

 

class Core{
 
public function __construct() {
 
}
 
public function testcore() {
return $this->atestone();
// OR
return $this->atesttwo();
}
 
public function __destruct() {
 
}
}

 

I'm looking to have a core class basically. Ideally, all classes can utilise other functions but my main aim is to have one core class that can use (freely) all other classes that are extended to it.

 

Is this possible?

 

Thanks in advance

 

James

Link to comment
Share on other sites

The reason to have a class that extends another class is to add some functionality to the parent class ie, the first class. Your goal of having a class called 'core' that will use all the classes extended from it is IMHO a bit backwards. Yes the 'core' class can be used by all your scripts but the functionality of the child classes that extend it will not be available unless you include them and construct objects of each child class. Then those items will have all the functionality of the specific class they belong to as well as the 'core' class.

Link to comment
Share on other sites

The reason to have a class that extends another class is to add some functionality to the parent class ie, the first class. Your goal of having a class called 'core' that will use all the classes extended from it is IMHO a bit backwards. Yes the 'core' class can be used by all your scripts but the functionality of the child classes that extend it will not be available unless you include them and construct objects of each child class. Then those items will have all the functionality of the specific class they belong to as well as the 'core' class.

 

Sorry, I didn't really explain too well.

 

I will be having a bootstrapper file that will include all the classes (class one, class two and so on) and the core. I would then like to use the core to use all other classes. To make life easier, simpler and to have less clutter, ect.

Link to comment
Share on other sites

Maybe a better question would be, what is the best way of using classes in a simple, easy, OOP manner? Bare in mind that many of my classes need to use functions from other classes to save cloning the same functions many times over.

 

Interfaces, abstracts, ect. I'm not fussed, just need as simple way of doing it.

 

EDIT: Forgot to mention, by OOP, I need the classes to be "chained". That would probably solve the problem of using functions from separate classes.

Edited by jamesmpollard
Link to comment
Share on other sites

Dependency Injection is the terms you are looking for.

class what {
 function __construct(ever $ever) {
  $this->ever = $ever;
 }
 
 public function DIme() {
 return $this->ever->me();
 }
}
 
class ever {
 public function me() {
 return 'Method in class ever.';
 }
}
 
$ever = new ever();
$what = new what($ever);
echo $what->DIme();
Edited by jcbones
Link to comment
Share on other sites

 

Dependency Injection is the terms you are looking for.

class what {
 function __construct(ever $ever) {
  $this->ever = $ever;
 }
 
 public function DIme() {
 return $this->ever->me();
 }
}
 
class ever {
 public function me() {
 return 'Method in class ever.';
 }
}
 
$ever = new ever();
$what = new what($ever);
echo $what->DIme();

Is there a way of making it so that the classes are not all in one file?

Link to comment
Share on other sites

There is no reason you should have one class that does and knows everything. It's far better to create specialised classes for your pages. It's also far better for performance as you'll quickly be loading too many unneeded classes.

Agreed. A crazy man would make one giant class for the whole site, benchmarking would be a disappointing result lol.

Link to comment
Share on other sites

As long as you realize that if 'core' is your master class and 'one' and 'two' are classes that extend 'core', you can't use just 'core' and access the methods that classes 'one' or 'two' define. You made it sound like that in your reply to my post.

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.