473,787 Members | 2,932 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

References between nested objects (PHP4)

Hello

In a content administration tool I call classes from inside classes in order
to separate the admin functions from the display-only functions:

class book
{
var $title;
var $author;
var $adminfunctions ;

function book($admin = false)
{
if ($admin) {
$this->adminfunctio ns =& new book_admin($thi s);
}
}

function display_book()
{
return $this->author.": ".$this->title;
}
}

class book_admin
{
var $boss;

function book_admin(&$bo ss)
{
$this->boss = $boss;
}

function update_book($au thor, $title)
{
$this->boss->author = $author;
$this->boss->title = $title;
}

[lots of other methods]
}

The admin code will be something like:

$book =& new book(true);
$book->adminfunctio ns->update_book("M ichael Crichton", "Jurassic Parc");
echo $book->display_book() ;

As you see there is a circle reference, as book_admin::bos s is a reference
back to book. I assume this is bad, and actually there are some problems
that I suspect are originated there.

Is there another possibility to access properties of the calling object? I
did not find any syntax similar to the parent::propert y syntax, and I can't
use inheritance, as there is already a vertical inheritance (such as page
extends book extends library...), and it is not possible to inherit two
classes.

Thanks for a hint!
Markus
Sep 5 '05 #1
13 3423
Reference is kinda kooky in PHP 4. I always try to dissuade people from
using it.

In the book_admin constructor, you need $this->boss =& $boss.
Otherwise the object will end up with a copy of its container.

Sep 6 '05 #2
> In a content administration tool I call classes from inside classes in order
to separate the admin functions from the display-only functions:

class book
{
var $title;
var $author;
var $adminfunctions ;

function book($admin = false)
{
if ($admin) {
$this->adminfunctio ns =& new book_admin($thi s);
}
}

function display_book()
{
return $this->author.": ".$this->title;
}
}

class book_admin
{
var $boss;

function book_admin(&$bo ss)
{
$this->boss = $boss;
As Chung Leong pointed out you should probably change the line
above to

$this->boss =& $boss;

}

function update_book($au thor, $title)
{
$this->boss->author = $author;
$this->boss->title = $title;
}

[lots of other methods]
}

The admin code will be something like:

$book =& new book(true);
$book->adminfunctio ns->update_book("M ichael Crichton", "Jurassic Parc");
echo $book->display_book() ;

As you see there is a circle reference, as book_admin::bos s is a reference
back to book. I assume this is bad, and actually there are some problems
that I suspect are originated there.
Circular references are not something bad in general. Reference to "parent"
in a "child" and to "children" from "parent" are usually ok.

Is there another possibility to access properties of the calling object? I
did not find any syntax similar to the parent::propert y syntax, and I can't
use inheritance, as there is already a vertical inheritance (such as page
extends book extends library...), and it is not possible to inherit two
classes.


You are using some strange inheritance (as you described it, but it's not
in your example code). Book should not extend page, page should not extend
book. Book should contain pages (have references to them), page should (or
could) have reference to the book. Same with library and books. In your
scenario each page can be treated as a book or a library and each book can
be treated as a library. You should probably also not use inheritance
between book and book admin.
Hilarion
Sep 6 '05 #3
Chung Leong wrote:
Reference is kinda kooky in PHP 4. I always try to dissuade people
from using it.

In the book_admin constructor, you need $this->boss =& $boss.
Otherwise the object will end up with a copy of its container.


Thank you very much for this comment - this does indeed solve the problems I
had!

Anyway my goal is improving performance at the front end (implementing PEAR
Cache did a lot there), and I am not sure if separating the admin functions
is actually worth the effort. I would highly appreciate some quick comments
about this:

Existing structure: Class trees based on common base classes, such as:

- class common_function s
- class entity extends common_function s

- class rubric extends entity
- class page extends rubric

- class gallery extends entity

- class picture extends entity

Class common_function s contains some methods used for display and lots of
methods used for administration (such as string formatting, outputting form
fields and other stuff), and also integrates the database, language and
configuration objects.
Class entity controls the dependencies between it's sub-objects.
Class entity and it's subclasses all have 1 method for retrieving data and 3
methods for administration (create/update, duplicate, delete).

Thus, also for the front-end scripts all the administration methods are
loaded. So I thought of 2 possible levels of separation:

1. The admin methods are separated only from class common_function s and
integrated as a separate object. The $boss reference can be avoided there,
as those methods do either directly address the database or return values
instead of setting class properties. But the admin methods of the
sub-objects will still be loaded by the front-end scripts.

2. The admin methods are separated from all classes and build a separate
class tree:
- class entity_admin extends common_function s_admin
- class rubric extends entity_admin
and so on; the the main tree subclass constructor tells it's parents which
admin class has to be loaded.

I implemented solution 2 now and compared the execution times of the most
time-consuming front-end page (a gallery overview, creates 1 entity, rubric,
page and gallery object and 28 picture objects for the thumbnails). There is
no significant difference; it takes between 2.0 and 2.3 seconds[1] (without
caching of course). On the other side, the admin scripts run slower.

[1] Yes, I have some more ideas where to reduce server load...

So what I would like to know is: Does it make a significant difference if
only about 200 lines of code are loaded instead of about 1000 lines? Or is a
possible performance gain compensated by the more complex structure?
Now this has got a lot of text - if somebody takes his/her time to give me a
comment about it: Thanks a lot in advance!
Markus
Sep 6 '05 #4
Hilarion wrote:

As you see there is a circle reference, as book_admin::bos s is a
reference back to book. I assume this is bad, and actually there are
some problems that I suspect are originated there.


Circular references are not something bad in general. Reference to
"parent" in a "child" and to "children" from "parent" are usually ok.


Thank you, I am happy to read this!
Is there another possibility to access properties of the calling
object? I did not find any syntax similar to the parent::propert y
syntax, and I can't use inheritance, as there is already a vertical
inheritance (such as page extends book extends library...), and it
is not possible to inherit two classes.


You are using some strange inheritance (as you described it, but it's
not in your example code). Book should not extend page, page should
not extend book. Book should contain pages (have references to them),
page should (or could) have reference to the book. Same with library
and books. In your scenario each page can be treated as a book or a
library and each book can be treated as a library. You should
probably also not use inheritance between book and book admin.


Yes I am sorry the example code was a poor example; I was quite tired and
annoyed when I wrote it. The classes actually behave the way you say they
should:

- common_function s
- entity extends common_function s
- "thing" extends entity

"Thing" will be the class to be actually instanciated, while some "things"
can have subclasses (as page extends rubric, because a page needs all the
properties and methods that a rubric has), and the relations between the
objects are handled at entity level via parent and order properties - thus
theoretically every kind of "thing" could be assigned to every other.

Thank you for your clarifications.

Markus
Sep 6 '05 #5
> Yes I am sorry the example code was a poor example; I was quite tired and
annoyed when I wrote it. The classes actually behave the way you say they
should:

- common_function s
- entity extends common_function s
- "thing" extends entity

"Thing" will be the class to be actually instanciated, while some "things"
can have subclasses (as page extends rubric, because a page needs all the
properties and methods that a rubric has),
Consider using common base class (eg. "document_fragm ent") for "rubric"
and "page" instead of inheritance between those.

and the relations between the
objects are handled at entity level via parent and order properties - thus
theoretically every kind of "thing" could be assigned to every other.


You should be more type-safe. I mean if the reference is always to an object
of specific class and is mostly used as the class, then you should make
sure, that it's always referred to as this class, not some base class.
Eg. you should use type hinting in PHP 5 and "instanceof " operator (in PHP 5)
or "is_a" function (in PHP 4 or 5).
Hilarion
Sep 6 '05 #6
Markus Ernst wrote:
Anyway my goal is improving performance at the front end (implementing PEAR
Cache did a lot there), and I am not sure if separating the admin functions
is actually worth the effort. I would highly appreciate some quick comments
about this:

Existing structure: Class trees based on common base classes, such as:

- class common_function s
- class entity extends common_function s

- class rubric extends entity
- class page extends rubric

- class gallery extends entity

- class picture extends entity


I'm an advocate for keeping things simple and obvious. Unless there's a
good reason to use inheritance, I generally avoid it. It's easier to
think that a book is a book, than a book being an extension of some
generalized, abstract object.

Sep 6 '05 #7
Chung Leong wrote:

I'm an advocate for keeping things simple and obvious. Unless there's
a good reason to use inheritance, I generally avoid it. It's easier to
think that a book is a book, than a book being an extension of some
generalized, abstract object.


Yes, this is a very useful guideline one sometimes tends to forget. Anyway
in this case the generalized abstract object is useful for handling the
relations between different things - i.e. a text element can be assigned to
a page, or to an author (biography), or to a book (abstract). A page can be
assigned to a rubric, or to an author (extended biography, works list...),
or to a book (review, excerpt...). Therefore I decided to use one
centralized ID and relations handling.
Sep 7 '05 #8
Hilarion wrote:

You should be more type-safe. I mean if the reference is always to an
object of specific class and is mostly used as the class, then you
should make sure, that it's always referred to as this class, not
some base class. Eg. you should use type hinting in PHP 5 and "instanceof "
operator
(in PHP 5) or "is_a" function (in PHP 4 or 5).


I see I got myself into some complicated things here and will have to spend
some more time with the manual :-)
Sep 7 '05 #9
Markus Ernst wrote:

Yes, this is a very useful guideline one sometimes tends to forget. Anyway
in this case the generalized abstract object is useful for handling the
relations between different things - i.e. a text element can be assigned to
a page, or to an author (biography), or to a book (abstract). A page can be
assigned to a rubric, or to an author (extended biography, works list...),
or to a book (review, excerpt...). Therefore I decided to use one
centralized ID and relations handling.


Markus,

But herein lies the problem. Inheritance is not applicable here. A
text element is not a "type of" a page. Rather, a page "contains" one
or more text elements.

A biography is a "type of" a book, so it would be applicable to derive
biography from book. The book also "has an" author, but the author is
not a "type of" text element (or vice versa).

Applying inheritance correctly can be quite complex - and I've seen it
misused quite a bit.

I use inheritance a fair amount. For instance - one I'm working on now
is for a non-profit. I have a hierarchy:

Person -> Member -> Board Member

Person includes non-members who have attended meetings and otherwise
shown an interest in the organization. Members are Persons with a
Member ID number and an expiration date. Board Member has the
additional attribute of Position.

Unfortunately, MySQL isn't an OO DB, so I need to fudge a little. I
could, for instance, put a Position column in the Persons table. But
that would end wasting space for those who are non-members.

So, I assign every person an internal ID (auto increment column). I
have a Members table with the ID, membership number which is linked to
the Person table. And a BoardMember table contains the ID and position.

The beauty of this is that all the work is hidden. If, for instance, I
want to send an email to everyone, I use a PersonList object. For
Members, a MemberList object, and Board Members I create a
BoardMemberList object. *All* the rest of the code to send the emails
is exactly the same. It's just a matter of which object I create. In
fact, the creation of the object is similar to:

switch($POST['mailto']) {
case 'All' :
$list=new PersonList();
break;
case 'Members':
$list = new MemberList();
break;
case 'Board':
$list = new BoardMemberList ();
break;
default:
$emsg = 'No list selected';
break;
}
(rest of processing)

This is a perfect *applicable* use for inheritance.
--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
Sep 7 '05 #10

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

Similar topics

6
1893
by: PIII450 | last post by:
Hi all, I have a system running IIS en Apache. It is also running ASP and PHP. Why would <% Dim obj1 Dim var1
5
9500
by: Jan Pieter Kunst | last post by:
(apologies if this message is a duplicate -- my news server seems to have problems) Greetings, When using PHP 4, this: // ex. 1 class A { function A(&$obj) {
29
1928
by: Tim Clacy | last post by:
Am I correct in think that you can't re-assign a reference to a different object? If so, what should happen here: struct A { DoSomeThing(); }; A& GetNextA();
8
3504
by: Eric Eggermann | last post by:
I'm having a problem with really large file sizes when serializing the classes that describe my little document. There are some circular references which result in the same object getting written to disk multiple times. Now I'm using just basic serialization as described in MSDN. Clearly, I need to stop serializing these parent references, but I do need to re-instate them to the proper objects when de-serializing. Can anyone help me with a...
37
2792
by: Tim N. van der Leeuw | last post by:
Hi, The following might be documented somewhere, but it hit me unexpectedly and I couldn't exactly find this in the manual either. Problem is, that I cannot use augmented assignment operators in a nested scope, on variables from the outer scope: PythonWin 2.4.3 (#69, Mar 29 2006, 17:35:34) on win32. Portions Copyright 1994-2004 Mark Hammond (mhammond@skippinet.com.au) -
28
2039
by: Frederick Gotham | last post by:
When I was a beginner in C++, I struggled with the idea of references. Having learned how to use pointers first, I was hesitant to accept that references just "do their job" and that's it. Just recently, a poster posted looking for an explanation of references. I'll give my own understanding if it's worth anything. First of all, the C++ Standard is a very flexible thing. It gives a mountain of freedom to implementations to do things...
3
1295
by: howa | last post by:
according to the php manual, it said: Do not use return-by-reference to increase performance, the engine is smart enough to optimize this on its own. I doubt if this only apply to the PHP5 engine, while if I am using PHP4, performance is a factor to continue to use return by refercnece?
2
1465
by: amygdala | last post by:
I'm trying to figure out the correct use of references. Lets say I have a class Validator and a class User. And I want to let a User instance be an referenced object in Validator. Where would the best place be to put the & sign, and why? (See the /* */ comments inside code) class Validator { public function __construct( /* here? */ $caller ) {
4
1699
by: Ronald Raygun | last post by:
I want to store objects in ana array and then iterate through the array, retrieving a (reference) to each object in the array, and calling a method on the array. I am not sure how to do it, but this is pseudocode of what I want to do: <?php $m_fieldItems = createObjects(); $outstr = ''; for($i=0; $i < count($m_fieldItems); $i++)
0
9655
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
9497
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
10169
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...
1
10110
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
9964
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
8993
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
7517
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
5398
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...
3
2894
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.