howto simulate packages/namespaces. create private classes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

howto simulate packages/namespaces. create private classes

Post by jmut »

Any idea how could I simulate either "functionality" in php.
Last edited by jmut on Thu Aug 31, 2006 1:15 am, edited 1 time in total.
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Post by Dale »

eh? :?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

It's usually done in pseudo fashion:

Code: Select all

class nameSpace_className....
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Yep. The main limitation with this is that class names get big really fast, and there's no `use namespace` to come to the rescue.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

Hmmmm...I haven't tried it...good thing...but I thought PHP5 supported nested classes???

Oh well... :lol:
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Hockey wrote:Hmmmm...I haven't tried it...good thing...but I thought PHP5 supported nested classes???
Just to squelch that myth:

Code: Select all

[feyd@home]>php -r "class foo{ class bar{} }"
Parse error: parse error, unexpected T_CLASS, expecting T_FUNCTION in Command line code on line 1

[feyd@home]>php -v
PHP 5.1.5 (cli) (built: Aug 15 2006 23:54:56)
Copyright (c) 1997-2006 The PHP Group
Zend Engine v2.1.0, Copyright (c) 1998-2006 Zend Technologies
    with Xdebug v2.0.0beta6, Copyright (c) 2002, 2003, 2004, 2005, 2006, by Derick Rethans
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

I don't know if you have followed the terms 'packages' or 'namespace' as used over the years. I assume that you mean the Java/C# style that derive from Ada. I would recommend taking some time to read the lengthy discussions within the PHP Group about implementing some kind of functionality like this. I think those who think that this is a 'super cool' and 'must have' feature may be sorely disappointed at what is finally implemeted in PHP. And the idea that it gets rid of long names have not seen the reality in other languages where the fully qualified name still often needs to be used.
(#10850)
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

arborint wrote:I don't know if you have followed the terms 'packages' or 'namespace' as used over the years. I assume that you mean the Java/C# style that derive from Ada. I would recommend taking some time to read the lengthy discussions within the PHP Group about implementing some kind of functionality like this. I think those who think that this is a 'super cool' and 'must have' feature may be sorely disappointed at what is finally implemeted in PHP. And the idea that it gets rid of long names have not seen the reality in other languages where the fully qualified name still often needs to be used.
What more could a namespace be, other than an additional errr..ummm....namespace :P

Just offers an additional level of scope resolution...

Like nesting classes, but without performance penalties...at least I wouldn't think so in compiled languages :P
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

You'll have to do some reading to find out.
(#10850)
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

LOL

There is a petition to have namespaces in PHP :P
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Hockey wrote:There is a petition to have namespaces in PHP :P
I'm pretty sure he mentioned that...
arborint wrote:I would recommend taking some time to read the lengthy discussions within the PHP Group about implementing some kind of functionality like this. I think those who think that this is a 'super cool' and 'must have' feature may be sorely disappointed at what is finally implemeted in PHP.
Yes, yes he did mention that.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

Ok...but he didn't say *petition* just disscussions...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

splitting hairs.
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post by jmut »

ok, maybe I didn't ask the right question.
What I really want is to separate a system on different subsystems(packages).
Let say this could be achieved with a proper naming convention of classes etc.
The problem actually is...how I define for example several classes as interface for a certain package(so other packages use ONLY this one/two classes and are NOT able to see/use the other classes.).

Something like...how to simulate private classes declaration (like in JAVA - I am still noob in java but think they have such thing).
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Write a class/interface that will act as a superclass/superinterface by implementing/referencing all the subclasses/subinterfaces. Use type hinting to require this superclass/superinterface.
Post Reply