473,903 Members | 5,463 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 #1
10 12652

"Average_Jo e" <jo*@geniegate. com> wrote in message
news:sl******** ********@pong.t unestar.net...
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


Don't know if such mechanism thing exists in PHP5. You can always use uniqid
to generate a unique class name, then define the class with eval. If the
name is kept private, then the class is private.

$class = uniqid("Inner") ;
eval(<<<CLASS

class $class {
}

CLASS
);
Jul 17 '05 #2
Chung Leong wrote:
Don't know if such mechanism thing exists in PHP5. You can always use uniqid
to generate a unique class name, then define the class with eval. If the
name is kept private, then the class is private.

$class = uniqid("Inner") ;
eval(<<<CLASS

class $class {
}

CLASS
);


That would be not only one of the ugliest hacks I've ever seen, it also
wouldn't be very effective. You can get all declared classes via
http://php.net/get_declared_classes. Security by obscurity by itself
isn't a good idea IMHO.

I've been thinking about this problem a while ago and came to the
conclusion it wasn't possible.

--
Pieter Nobels
Jul 17 '05 #3
Average_Joe wrote:
Something like:

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

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


OK, let's see - I don't have PHP5 handy to test it right now, but try
something like this:

class factory {

function __construct() {
$tr = debug_backtrace ();
if($tr[1]['class'] != 'A' || $tr[1]['function'] != 'getObject')
throw new Exception;
}

function getObject() {
self::$instance = new factory;
return(clone(se lf::$instance)) ;
}
}

$obj = factory::getObj ect();

Again, this is untested - so I don't know if it will work but may be worth
trying. Since I don't know what the purpose of this is, I can't provide any
more feedback to suggest other methods of accomplishing it.
Jul 17 '05 #4
Zurab Davitiani wrote:
if($tr[1]['class'] != 'A' || $tr[1]['function'] !=

'getObject'

This should read
if($tr[1]['class'] != 'factory' || $tr[1]['function'] != 'getObject'
Jul 17 '05 #5
"Pieter Nobels" <pi******@MOVEp cmania.be> wrote in message
news:_p******** *************** @phobos.telenet-ops.be...
Chung Leong wrote:
Don't know if such mechanism thing exists in PHP5. You can always use uniqid to generate a unique class name, then define the class with eval. If the
name is kept private, then the class is private.

$class = uniqid("Inner") ;
eval(<<<CLASS

class $class {
}

CLASS
);


That would be not only one of the ugliest hacks I've ever seen, it also
wouldn't be very effective. You can get all declared classes via
http://php.net/get_declared_classes. Security by obscurity by itself
isn't a good idea IMHO.

I've been thinking about this problem a while ago and came to the
conclusion it wasn't possible.


This is PHP after all. The source code to the class would be available so
it'd be just as easy to remove the "private" keyword in order to access the
private variable. The use of private method/classes has nothing to do with
security so I don't know what you're driving at.
Jul 17 '05 #6
"Zurab Davitiani" <ag*@mindless.c om> wrote in message
news:6A******** ******@newssvr2 1.news.prodigy. com...
Zurab Davitiani wrote:
if($tr[1]['class'] != 'A' || $tr[1]['function'] !=

'getObject'

This should read
if($tr[1]['class'] != 'factory' || $tr[1]['function'] != 'getObject'


Very clever solution. One tiny problem: PHP collapses function names to
lower case, so the comparison should be to 'getobject'.
Jul 17 '05 #7
Chung Leong wrote:
"Zurab Davitiani" <ag*@mindless.c om> wrote in message
news:6A******** ******@newssvr2 1.news.prodigy. com...
Zurab Davitiani wrote:
> if($tr[1]['class'] != 'A' || $tr[1]['function'] !=

'getObject'

This should read
if($tr[1]['class'] != 'factory' || $tr[1]['function'] != 'getObject'


Very clever solution. One tiny problem: PHP collapses function names to
lower case, so the comparison should be to 'getobject'.


That's no longer true for PHP5, unless it's some part of optional PHP4
compatibility mode. PHP5 class and function/method names are in their
proper case.

I finally got around to trying this solution in PHP5, and it worked OK - one
thing missing is a static $instance property definition inside the class. I
still don't get what is the point of this though - maybe developers who
don't read docs - enforce factory class programatically ?
Jul 17 '05 #8
In article <rK************ ********@comcas t.com>, Chung Leong wrote:
"Pieter Nobels" <pi******@MOVEp cmania.be> wrote in message
news:_p******** *************** @phobos.telenet-ops.be...
Chung Leong wrote:
> Don't know if such mechanism thing exists in PHP5. You can always use uniqid > to generate a unique class name, then define the class with eval. If the
> name is kept private, then the class is private.
>
> $class = uniqid("Inner") ;
> eval(<<<CLASS
>
> class $class {
> }
>
> CLASS
> );
>
>


That would be not only one of the ugliest hacks I've ever seen, it also
wouldn't be very effective. You can get all declared classes via
http://php.net/get_declared_classes. Security by obscurity by itself
isn't a good idea IMHO.

I've been thinking about this problem a while ago and came to the
conclusion it wasn't possible.


This is PHP after all. The source code to the class would be available so
it'd be just as easy to remove the "private" keyword in order to access the
private variable. The use of private method/classes has nothing to do with
security so I don't know what you're driving at.


Seems like no matter how hard we try to squelch the right brain in
programming.. a persons artistic side has to come out. :-)

The idea of random generated class names is artistic to some, but ugly
to others. Thats my theory anyhow. :-)

In *my* case, it wouldn't work, since I need the inner class to have
access to private variables and methods in it's envelope class.

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

Jul 17 '05 #9
In article <fn************ ****@newssvr27. news.prodigy.co m>, Zurab Davitiani wrote:
I finally got around to trying this solution in PHP5, and it worked OK - one
thing missing is a static $instance property definition inside the class. I
still don't get what is the point of this though - maybe developers who
don't read docs - enforce factory class programatically ?


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)

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().

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)

Aside from this, PHP5 is looking really _really_ good. :-)

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

Jul 17 '05 #10

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

Similar topics

10
4560
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
2439
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
1797
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
1980
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
2842
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
16953
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
2070
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
2291
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
9999
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...
1
10983
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10500
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...
1
8048
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
7206
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
6088
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4308
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3324
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.