Well, if you're using PHP5, you can use Abstract Classes...but I really don't like the idea of using abstract classes...decide for yourself:
http://us.php.net/manual/e
Main Topics
Browse All TopicsI have a "main" class which needs to handle a particular object internally. This "internal" object has no other purpose than to be used by this main class. It would be nice if I could package them together in 1 file.
Of course, I could just combine the class definitions in one file. However, what's stopping others from instantiating this "internal" object?
Also, there is a constant defined in the main class that I need access to from the internal object. Is there a way I can share this information from the main object to the internal object? Creating a superclass containing the constant and having the two classes extend from it seems like overkill to me.
So:
1. How do I create a class so that only a particular class can use it (and nothing else can instantiate it)?
2. How can I share a constant member to this internal object?
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Well, if you're using PHP5, you can use Abstract Classes...but I really don't like the idea of using abstract classes...decide for yourself:
http://us.php.net/manual/e
abstract class internalObj{
static $property = 'whatever';
protected function testFunc()
{
echo $this->property;
}
}
class mainClass extends internalObj{
public function doSomething(){
parent::testFunc();
}
}
$x = new mainClass();
$x->doSomething(); /* output whatever */
$y = new internalObj(); /* fail */
Abstract class is not the solution. The main class isn't supposed to extend the "internal" class. This is not a problem solved by inheritance.
What I am trying to do is limit the scope and use of internal class to just the main class. Added to the problem, I want the internal class to have access to a property in the main class.
If you use a class constant you can access the constant fromoutside the class.
eg:
class Blah {
const FRED = 23.5;
function __constuct(){
}
protected function do_something(){
}
}
you can reference the constant staticaly. (That means you do not need to create an instance of the class. you can also do this with static methods. Static anything can not change though... ie you cant create many instances of them, or change them)
so to access your class constant:
require_once(Blah.php); //asuming your class is called Blah.php
$x = Blah::FRED;
sounds like you want to extend the main class to add a little more functionality.
eg:
class Big_blah extends Blah{
public function do-stuff(){
$x = Blah::FRED;
return 2*$x;
}
}
note:
a public function can be seen by everyone
a private function can only be used by the class that it was written in
a protected function is like a private function, except classes that are extended from it can also use it.
class constants are ALWAYS public.
Business Accounts
Answer for Membership
by: BrianGEFF719Posted on 2008-05-18 at 21:08:28ID: 21595174
>>1. How do I create a class so that only a particular class can use it (and nothing else can instantiate it)?
Not Really.
>>2. How can I share a constant member to this internal object?
Why don't you just define a static variable in the main class that inherits the "internal object"
class internalObj {
static $property = 'whatever';
}
class mainClass extends internalObj {
public function doSomething()
{
echo parent::property;
}
}