473,779 Members | 2,072 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

inner "nested" classes?

Hello PHP people,

Was wondering if PHP5 had some sort of "nested class" functionality.

I'm trying to figure out how to return an object (with a private
constructor) that has access to variables in another class.

Something like:

$obj = $factory->getObject("123 4");

Where getObject() is the ONLY way to get an instance of $obj.

$obj has access to private data from the $factory object.

Kind of like inner and anonymous classes in java I guess.

Any approach to doing this in PHP5?

Jamie
--
http://www.geniegate.com Custom web programming
User Management Solutions Perl / PHP / Java / UNIX

Jul 17 '05
10 12633
Average_Joe wrote:
Simple example:

Say you've got:

<?php
class Invoice() { .. }

class Item() { ... }

// You want to do:

$item = $invoice->getItem("P100" );

echo $item->title();

echo $item->getInvoice()->title(); // get the invoice title.

?>

The idea here is that at no point can anything except Invoice() generate
an instance of Item. (Item has a private constructor)
OK, that's close to how I imagined it as well. In this case, you would make
sure that Item constructor was only called from Invoice class, or throw an
exception. I cannot think of another way to do this other than with
debug_backtrace which is really a workaround (or a hack) for this, not a
language feature.

Since the check would be already done, the Item constructor need not be
private in this case. Indeed if it was, Invoice wouldn't be able to
construct it since the call would come from global namespace.
Furthermore. Say Item is an interface or abstract class, the actual items
are different, but all of them implement an "Item" interface. This way,
you could have a Clothing Item, Electronics Item, etc.. each with
methods specific to the item type. You also can be reasonably assured
that all Item()'s haven't been constructed outside an Invoice().
In order to make sure this works with abstract classes, you would have to
call the parent constructor explicitly. Also, you would have to allow the
parent constructor to complete instantiation - i.e. not throw an exception
- when it's called from a __construct function of a class whose parent
class is "Item". You can use get_parent_clas s for that.

Also, it's not pretty but every derived class would have to have a similar
check in its constructor. You may create a common method for this in parent
that's inherited by derived classes, but backtrace values would have to
come from constructor function and passed to that method.
You know when working with the code that each Item must have a parent
Invoice instance, down the road, if an Item required access to stuff in
the Invoice class (such as a is_sales_tax() method ?) You would be able
to access it, w/out giving access to other areas of the application. (So
that if is_sales_tax() were to change, (lets say sales tax became
dependant on country or something) you would know it was accessed ONLY
in Invoice() and any Item's it generated, making changes easier)
This is more or less easier to implement. When calling the Item constructor
from Invoice (or through a separate method), pass $this and set it as
Item's parent property; then refer back to it from the Item class by
calling $this->getParent() or $this->parent.
Aside from this, PHP5 is looking really _really_ good. :-)


I would have preferred multiple inheritance to interfaces.
Jul 17 '05 #11

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

Similar topics

10
4551
by: Paul Morrow | last post by:
I'm hoping that someone can explain why I get the following exception. When I execute the code... ###################################### class Parent(object): class Foo(object): baz = 'hello from Parent.Foo' class Child(Parent): #Foo.baz = 'hello from Child.Foo'
3
363
by: Vinodh Kumar P | last post by:
I abstract a Car, and the parts wihtin a car, with a C++ class. Since only my version of class Car uses the classes defined for its parts, is it a good practice to do like this? class Car { class Wheel {
5
2430
by: Fabio Rossi | last post by:
Hi! I'm learning C++ from the book "Thinking in C++". Now I'm reading about nested classes and access control. I have written this code #include <iostream> class Outer { private: int outer_data;
7
1788
by: Alfonso Morra | last post by:
I have a class that contains a nested class. The outer class is called outer, and the nested class is called inner. When I try to compile the following code, I get a number of errors. It is not obvious to me, where I'm going wrong (the compiler messages do not seem to make much sense). here is the code: outer class declared as ff in "outer.h":
4
1975
by: Christopher Ireland | last post by:
Hi -- I'm trying to find an example of a nested class implemented within the .NET Framework itself but without much success. I don't suppose that anybody knows of such an example off the top of their head, do they? Many thanks! -- Best Regards,
8
2835
by: Etienne Boucher | last post by:
Nested classes are usualy objects made to only live in their parent object instance. In other words... public class Outter { public class Inner { } }
8
16920
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
4
365
by: Sundararajan | last post by:
Dear Folk, I have implemented a nested class as follows: Class Enclosing { string strName; Class Nested1 { }
3
2064
by: Martin Skou | last post by:
I'm experimenting with using Python for a small web interface, using Mark Hammond's nice win32 extensions. I use a small class hierarchy which uses inheritance and nested classes. Here are a small extract of the code: class page: def __init__(self):
5
2284
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.
0
9474
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
10306
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
9931
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8961
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...
0
6727
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
5373
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5504
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3632
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2869
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.