473,609 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Nested classes in PHP?

Hi All,

I remember reading that both nested classes and namespaces would be
available in PHP5. I know that namespaces got canceled (much sadness...),
however, I *thought* that nested classes were still an option.

However, I am coming up dry looking for information on how to do this, and
the most recent references I am able to find to nested classes in PHP are
dated 2003. And they all say the same thing: sorry, can't do that.

So, it's not possible? Is that the end of the matter?

Any suggestions for attempting to implement namespaces in PHP5? Some sort
of hack or workaround?

Thanks for any thoughts.

-jb
Jul 17 '05 #1
26 29614
On Thu, 20 Jan 2005 22:21:16 GMT, "Joshua Beall"
<jb****@donotsp am.remove.me.he raldic.us> wrote:
I remember reading that both nested classes and namespaces would be
available in PHP5. I know that namespaces got canceled (much sadness...),
however, I *thought* that nested classes were still an option.


No, they got the chop too.

http://cvs.php.net/co.php/ZendEngine2/ChangeLog?r=1.622
Search for "nested class" on that page - there's an entry for zend.c stating
"Nested classes are gone."

--
Andy Hassall / <an**@andyh.co. uk> / <http://www.andyh.co.uk >
<http://www.andyhsoftwa re.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #2
"Joshua Beall" <jb****@donotsp am.remove.me.he raldic.us> wrote in message
news:w9WHd.2425 $Hg6.2404@trndd c09...
Hi All,

I remember reading that both nested classes and namespaces would be
available in PHP5. I know that namespaces got canceled (much sadness...),
however, I *thought* that nested classes were still an option.

However, I am coming up dry looking for information on how to do this, and
the most recent references I am able to find to nested classes in PHP are
dated 2003. And they all say the same thing: sorry, can't do that.

So, it's not possible? Is that the end of the matter?

Any suggestions for attempting to implement namespaces in PHP5? Some sort
of hack or workaround?

Thanks for any thoughts.

-jb


The workaround is to use an underscore.
Jul 17 '05 #3
What on earth is so special about a nested class? If you can include() a
class definition from within a class, then instantiate it, is that not good
enough?

--
Tony Marston

http://www.tonymarston.net

"Joshua Beall" <jb****@donotsp am.remove.me.he raldic.us> wrote in message
news:w9WHd.2425 $Hg6.2404@trndd c09...
Hi All,

I remember reading that both nested classes and namespaces would be
available in PHP5. I know that namespaces got canceled (much sadness...),
however, I *thought* that nested classes were still an option.

However, I am coming up dry looking for information on how to do this, and
the most recent references I am able to find to nested classes in PHP are
dated 2003. And they all say the same thing: sorry, can't do that.

So, it's not possible? Is that the end of the matter?

Any suggestions for attempting to implement namespaces in PHP5? Some sort
of hack or workaround?

Thanks for any thoughts.

-jb

Jul 17 '05 #4
Hi,

Using $_SESSION with a sub array would give you a a namespace.

With regard to obects within objects then it indicates a design problem.
Declaring an object within an object can be done, but the way to do it is
by defining an object as a class in itself and then allowing the container
class to have an array of objects.

Pseudo code
class (I am a invoice header class) {

class property (I am an array containing 0->n detail line objects)
}

class (I am an invoice detail line class) {

}



On Thu, 20 Jan 2005 22:21:16 +0000, Joshua Beall wrote:
Hi All,

I remember reading that both nested classes and namespaces would be
available in PHP5. I know that namespaces got canceled (much sadness...),
however, I *thought* that nested classes were still an option.

However, I am coming up dry looking for information on how to do this, and
the most recent references I am able to find to nested classes in PHP are
dated 2003. And they all say the same thing: sorry, can't do that.

So, it's not possible? Is that the end of the matter?

Any suggestions for attempting to implement namespaces in PHP5? Some sort
of hack or workaround?

Thanks for any thoughts.

-jb


Jul 17 '05 #5
"Tony Marston" <to**@NOSPAM.de mon.co.uk> wrote in message
news:cs******** ***********@new s.demon.co.uk.. .
What on earth is so special about a nested class? If you can include() a
class definition from within a class, then instantiate it, is that not
good enough?

--
Tony Marston


Sometimes you have a class that has no meaning/usefulness outside of it's
context in a parent class. Thus, it is polluting the global namespace with
information that will never be used (outside of that one specific class) to
define it in the global namespace.

Of course in most PHP apps this is not a problem because you don't have a
massive number of classes... but it would be nice to have nested classes or
namespaces.

-jb
Jul 17 '05 #6

"Joshua Beall" <jb****@donotsp am.remove.me.he raldic.us> wrote in message
news:SO8Id.5740 $J6.3455@trnddc 02...
"Tony Marston" <to**@NOSPAM.de mon.co.uk> wrote in message
news:cs******** ***********@new s.demon.co.uk.. .
What on earth is so special about a nested class? If you can include() a
class definition from within a class, then instantiate it, is that not
good enough?

--
Tony Marston
Sometimes you have a class that has no meaning/usefulness outside of it's
context in a parent class. Thus, it is polluting the global namespace
with information that will never be used (outside of that one specific
class) to define it in the global namespace.


I disagree that this is any kind of an issue. You may have a single class
definition, but none of the properties or methods of that class will clash
with anything else, so there is no problem.
Of course in most PHP apps this is not a problem because you don't have a
massive number of classes...
It does not matter how many classes you have in your entire application.
Each HTTP request is for a single page, therefore you need only load those
classes which are sufficient to satisfy that single request.
but it would be nice to have nested classes or namespaces.


The benefit of nested classes is not worth the effort. It is the same for
namespaces. I have programmed in languages for the past 25+ years without
ever using namespaces, so their lack of appearance in PHP is not a problem
for me. Exactly what problem do they solve, if any?

--
Tony Marston

http://www.tonymarston.net


Jul 17 '05 #7
"Tony Marston" <to**@NOSPAM.de mon.co.uk> wrote in message
news:cs******** ***********@new s.demon.co.uk.. .
but it would be nice to have nested classes or namespaces.


The benefit of nested classes is not worth the effort. It is the same for
namespaces. I have programmed in languages for the past 25+ years without
ever using namespaces, so their lack of appearance in PHP is not a problem
for me. Exactly what problem do they solve, if any?


Problem? I'm not sure they solve any sort real problem... But they make it
neater for a human being to look at your code and see what things are
grouped together.
Jul 17 '05 #8
>"Chris Newey" <newey...@hotma il.com>
Using $_SESSION with a sub array would give you a a namespace.
?
With regard to obects within objects then it indicates a design problem. Declaring an object within an object can be done, but the way to do it is by defining an object as a class in itself and then allowing the container class to have an array of objects.


I'm new to this group (I work with PHP, but my background is mostly
C++, Java, etc.), and this is the first thread I read in this group,
and either the terminology used in "PHP land" is rather different from
what I'm used to (not that likely, we subscribe to "PHP Architect", as
well), or there seems to be some confusion of class and object, here.

OP asked for the possibility of defining a class within another class
(a nested class). If that nested class is only available to the
enclosing class, then there wouldn't be a need for a new syntax for
accessing it from the outside (as in "new outer::inner(.. .)").

Having a nested class doesn't mean nesting objects inside of each
other: that's a misunderstandin g. It's simply an issue of scope: the
inner class might only be accessible to the enclosing class.

Secondly, this is not indicative of a design error, and to the contrary
may be good design, as, as OP said, it avoids polluting the global
namespace with classes that are only used locally. For example, if you
have a container class, then the iterator might be a nested class of
it, as the container is the only class needing to be able to
instantiate it.

Thirdly, if you really meant nesting objects, rather than nesting
classes, then do you really mean that having an object with instance
variables (nested objects) indicates a design error? That's nonsense.
Regards,

Terje

Jul 17 '05 #9
<ts*******@hotm ail.com> wrote in message
news:11******** **************@ c13g2000cwb.goo glegroups.com.. .
OP asked for the possibility of defining a class within another class
(a nested class). If that nested class is only available to the
enclosing class, then there wouldn't be a need for a new syntax for
accessing it from the outside (as in "new outer::inner(.. .)").


Glad there's somebody around here who doesn't think I'm nuts!

My background is C++ with a tiny bit of Java, too. Probably affects my
thinking on this matter.
Jul 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2397
by: Erik Bongers | last post by:
Hi, Nested classes only seem to be able to access static members of the surrounding class : class SurroundingClass { public: class InnerClass { public:
3
1204
by: Rubén Campos | last post by:
Organizing classes, types, structures, enums and whatever other entities into nested namespaces requires to include into every header and implementation file the complete path of namespaces. Let me show an example: classes.hpp classes.cpp util.hpp util.cpp main.cpp // classes.hpp namespace Classes { class MyBaseClass {
6
559
by: B0nj | last post by:
I've got a class in which I want to implement a property that operates like an indexer, for the various colors associated with the class. For instance, I want to be able to do 'set' operations like MyClass.MyColors = Color.Green or, a 'get', such as Color forecolor = MyClass.MyColors; I want to use an indexer so I can take parameters, such as the color type (e.g. "Foreground", "Background" etc.). With a single member function I couldn't...
8
16887
by: Robert W. | last post by:
I've almost completed building a Model-View-Controller but have run into a snag. When an event is fired on a form control I want to automatically updated the "connnected" property in the Model. This works fine if all of the properties are at the top (root level) of the model but I'd like to keep them in nested classes to organize them better. So, for example, part of my data model looks like this (simplified) : public class MainClass
2
2039
by: Bob Day | last post by:
Using VS2003, VB.NET, MSDE... I am looking at a demo program that, to my surprise, has nested classes, such as the example below. I guess it surprised me becuase you cannot have nested subs, and I am not sure why you would want nested classes anyway. Is there a URL that explains the advantages to nested classes, when they would be used, etc? What are your thoughts? Also, if a Class has nothing before it (i.e. no Public or Private,...
2
2363
by: miked | last post by:
I am architecting in a read only class for use in mapping data to a business object. The object makes strong use of nested classes and their ability to access protected fields. The downside is when a nested class inherits from it’s parent class you get this infinite class chain in intellisense when consuming the class. To get around this I created two child classes Reader and Writer which require a base Person object. When consuming...
5
2279
by: Jake K | last post by:
What purpose does nesting a class inside another class typically server? Are there conditions where this would be beneficial? Thanks a lot.
3
2116
by: Cousson, Benoit | last post by:
I don't think so; my original email was mainly a question. I do agree that they are other ways to do what I'm trying to achieve; there are always several ways to solve an issue. Few days ago, I decided to use nested class because I realized that it was the most convenient way to implement my need. Since this feature is supported in many languages, I was just surprised that Python did support it only partially, hence my original email. ...
0
143
by: Maric Michaud | last post by:
Le Tuesday 12 August 2008 23:15:23 Calvin Spealman, vous avez écrit : I was not aware of any "nested classes are unsupported" before and didn't consider nested classes as bad practice till now, even with the pickle limitation (not every class are intended to be pickled), more you didn't give any evidence or any pertinent quote of this and at least one of Guido in the above threads seems contradict you :...
2
2280
card
by: card | last post by:
Hi everyone, I have a question about referencing a nested class contained within a templated class. Of course the best way to show you is by example. Here's my templated classes: #include <stack> template <class T> class A { public:
0
8145
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8095
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8588
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8556
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7030
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6068
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5526
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4103
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1407
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.