delete an object ?

This is a discussion on delete an object ? within the PHP Language forums, part of the PHP Programming Forums category; Hi to all ;) I use OOP with PHP4 and i don't understand something. I have 2 classes VISIT & ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-24-2005
Fabrice Régnier
 
Posts: n/a
Default delete an object ?

Hi to all ;)

I use OOP with PHP4 and i don't understand something.

I have 2 classes VISIT & PLANNING like that:

class clVISIT {
var $when;
var $where;

//constructor
function clVISIT($_when,$_where) {
$this->when = $_when;
$this->where = $_where;
}
}

class clPLANNING {
var $who;
var $visit = array();

//constructor
function ClPLANNING($_who) {
$this->who = $_who;
for ($i=0;$i<10;$i++) { $visit = new clVISIT("foo".$i , "foobar".$i ) ; }
}

}

Ok, in the last constructor, we can see that i create new clVISIT
object. My question is: how can i delete some of them in the same
constructor ? is there a word like "new" but to delete ?

regards,

f.
Reply With Quote
  #2 (permalink)  
Old 03-24-2005
ZeldorBlat
 
Posts: n/a
Default Re: delete an object ?

First, if you're trying to do what I think you are, write:

{ $visit[] = new clVISIT("foo".$i , "foobar".$i ) ; }

Note the square brackets after $visit.

To delete an element from an array, just use unset. For instance, to
remove $visit[5], just write unset($visit[5]). This won't re-index the
array -- you'll just have buckets with null values. To re-index it in
a continious way, use:

$visit = array_merge($visit);

However, I'm wondering why you would create them in the first place if
you just intended to delete them...unless of course there's some other
things you do first that you haven't included in the example.

Reply With Quote
  #3 (permalink)  
Old 03-25-2005
Fabrice Régnier
 
Posts: n/a
Default Re: delete an object ?

Hi and thanx a lot ;)

> However, I'm wondering why you would create them in the first place if
> you just intended to delete them...unless of course there's some other
> things you do first that you haven't included in the example.

Right. I didn't write some lines to make the example clearer.

regards,

f.


Reply With Quote
  #4 (permalink)  
Old 03-25-2005
Chung Leong
 
Posts: n/a
Default Re: delete an object ?

"Fabrice Régnier" <regnier.fab@free.fr> wrote in message
news:42430a8d$0$32572$626a14ce@news.free.fr...

> Ok, in the last constructor, we can see that i create new clVISIT
> object. My question is: how can i delete some of them in the same
> constructor ? is there a word like "new" but to delete ?
>
> regards,
>


There is no concept of delete or destructor in PHP 4. PHP will delete an
object where it's no longer needed and (sometimes) free up the memory used
for it. You can reclaim memory (sometimes) by unsetting a variable but
there's no guarantee. Because PHP is used primarily for handling web
requests, it likes to free memory either after a script has finished and
(sometimes) not freeing it at all.

OOP in general should be used sparingly in PHP 4, only when there's a
clearly benefit of doing so. It is a weakness of the language. If you insist
on coding to the weakness of a language, you will only come to grief.


Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 10:36 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0