/    Sign up×
Community /Pin to ProfileBookmark

Two questions to the gurus

Hello,

I have got two questions for experienced PHP gurus:

1.) The first question is a bit about programming “style”.
Take a case where I have “abstract AClass1” and its’ child “Class1” and a third class “abstract Class2” which is a child of “class1”:

[CODE]
abstract AClass1 {…}

class1 extends AClass1 {…}

abstract class2 extends class1 {…}
[/CODE]

Now let’s say i want to seperate all abstract classes from the “real” classes in different files:

file for abstracts:

[CODE]
abstract AClass1 {…}

abstract Class2 extends Class1 {…}
[/CODE]

file for “real” classes:

[CODE]
Class1 extends AClass1 {…}
[/CODE]

Now obviously in the first file on the definition of “Class2”, “Class1” will be missing. And here goes my question: what is the best way to solve this?
What I do is I require the file needed between the two classes:

[CODE]
abstract AClass1 {…}

require_once(file for “real” classes)

abstract Class2 extends Class1 {…}
[/CODE]

This does work (because i have a seperate files for each “real” class) but seems rather ugly to me (doing a “require” out in the nowhere).
How would you do this?

2.) The second question is about “unset” and destructors:
How far does “unset” really go? I mean if you unset an object, is every sub object and array and everything really destroyed?
I am asking since PHP does not support destructors, so one should assume the answer to the above question is yes. But since there is the possibility to imitate destructors through “register_shutdown_function” I was wondering if it is perhaps still necessary to unset certain things “by hand”?

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@MindzaiApr 04.2009 — For your first question, I would recommed keeping *every* class in a separate (ie, its own) file, then autoloading as necessary.

As for question 2, PHP certainly does support destructors. Not sure what you mean by sub objects and arrays, but if you mean all properties of the object then the answer is yes, if you unset an object everything will be destroyed, including all properties of the object. It is not necessary to do anything more, although as I mentioned previously you can use the destructor to perform any pre-destruction tasks you might have for your object.
Copy linkTweet thisAlerts:
@deck42authorApr 04.2009 — Well thanks for your answer first of all.

Now as for autoload, that's really nice. Also I somehow missed that destructors are available since PHP5.

But my question about "sub" objects remains, let me put it in more exact words.

If I have an oject, which as a property has a pointer to another object. Like this:
[CODE]
class Class1 {
var pointer;
}
...
Class1Instance->pointer = new ClassXY;
...
[/CODE]


If I now unset "Class1Instance", of course "Class1Instance->pointer" will be destroyed. But what about the memory occupied by the actual instance of "ClassXY", will it be freed too?
Copy linkTweet thisAlerts:
@NogDogApr 04.2009 — An object's memory contents (just like that for a variable or a resource) will be cleaned up whenever there are no more references to it. So as long as no other object, variable, or function is holding a reference to that same object instance, it will be cleaned up when ClassInstance is unset.
Copy linkTweet thisAlerts:
@deck42authorApr 04.2009 — Alright, thanks alot!
Copy linkTweet thisAlerts:
@ZnupiApr 05.2009 — One subtle thing: considering what NogDog said, it is not necessary to unset() an object for its destructor to be called. It is enough to clear any references to it. Example
[code=php]<?php
class MyTestClass {

function __construct() {
echo "Class constructed.n";
}

function __destruct() {
echo "Class destructed.n";
}

}
$myObject = new MyTestClass();
$myObject = 123;
echo "Script ended.n";
?>[/code]

This will output:
Class constructed.
Class destructed.
Script ended.

The reason why I added the "Script ended" message was to make obvious the fact that the class is not destructed because the script has reached the end, but because the only reference to the class instance, $myObject, was assigned some other value.
Copy linkTweet thisAlerts:
@deck42authorApr 05.2009 — Thanks I see. Basically this is what I am relying on when "unset"-ing an object that contains a pointer to another object: Just the pointer will be unset, the other object will be destroyed the way you show.
Copy linkTweet thisAlerts:
@ZnupiApr 05.2009 — Thanks I see. Basically this is what I am relying on when "unset"-ing an object that contains a pointer to another object: Just the pointer will be unset, the other object will be destroyed the way you show.[/QUOTE]
(!) The other object will be destroyed only if there are no other pointers to it.
×

Success!

Help @deck42 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...