Re: References to methods of parent object

From: Date: Tue, 13 Jun 2000 05:18:17 +0000
Subject: Re: References to methods of parent object
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-1692@lists.php.net to get a copy of this message
Remember your scope. When you make the callback local (automatic variable in C/C++) it'll go out of scope and be deleted. You -MAY- be able to get around this since PHP4 uses ref counting and garbage collection by hanging onto a reference to the parent instead of a pointer to it. (Check the & operator) I don't know when the reference would go out of scope, though... 'Luck. -Szii At 11:14 PM 6/12/00 -0400, Peter Bowen wrote: >I am trying to call a method of a parent object and failing to see how it >can be done. I have seen several similar references, but none that >address >this problem. I am instanciating a class, and in the constructor >function, >I am instanciating the callback class as a local variable. I want the >callback class to call methods of the current class instanciation. Is >there a way to do this? I have attached the relevant code below. > >Thanks. >Peter Bowen > > > ><?php >require("callback.class.php"); > >class asx { > var $entrylist; //array of polymorphic objects using base class >entry; > var $ac; //int; current entry of array > var $status; //string; last element, if in a block > > function asx($fh) { > $this->ac = 0; > $this->status = ""; > > $cb = new string_callback; > $cb->parent = $this; > > $cb->register("/<REF(>|(\s+[^>]*>))/i","\$this->parent->ref",2); > $cb->register("/<ENTRY(>|(\s+[^>]*>))/i"," >\$this->parent->openentry",1); > $cb->register("/<ENTRYREF(>|(\s+[^>]*>))/i"," >\$this->parent->entryref"); > > $cb->register("/<\/ENTRY[^>]*>/i","\$this->parent->closeentry"); > > $n=0; > while ($fc = fgets($fh,1024)) { > $n++; > $cb->string($fc,$n); > } > } > > function openentry($text, $lineno) { > if (empty($this->status)) { > $this->status = "entry"; > $this->ac = sizeof($this->entrylist) + 1; > $this->entrylist[$this->ac]=new asxmisc; > $this->entrylist[$this->ac]->start = $lineno; > $this->entrylist[$this->ac]->type = T_ENTRY; > $this->entrylist[$this->ac]->source = F_ORIG; > } else { > die("Invalid File"); > } > } > > function closeentry($text, $lineno) { > if ($this->status=="ref") { > $this->status = ""; > $this->entrylist[$this->ac]->end = $lineno; > } else { > die("Invalid File"); > } > } > > function ref($text, $lineno) { > if ($this->status=="entry") { > unset($pcrear); > $this->status = "ref"; > $this->entrylist[$this->ac]->fromstring($text); > } elseif ($this->status=="ref") { > unset($pcrear); > } else { > die("Invalid File"); > } > } > > function entryref($text, $lineno) { > $delete = FALSE; > if ($this->status=="") { > $this->ac = sizeof($this->entrylist) + 1; > preg_match("/ref\s*=\s*(?:(?:\"([^\"]*)\")|([^>]*)). >*\/\s*>/i",$text,$pcrear); > if (!empty($pcrear[1])) { > $tstr = $pcrear[1]; > } else { > $tstr = $pcrear[2]; > } > unset($pcrear); > preg_match("@/([^/\?][^/\?]*)\?@",$tstr,$pcrear); > if ($pcrear[1]=="register.php") { > $this->entrylist[$this->ac]=new asxmisc($tstr); > $this->entrylist[$this->ac]->type=T_REGISTER; > } elseif ($pcrear[1]=="getad.php") { > $delete = TRUE; > $this->entrylist[$this->ac]=new asxmisc($tstr); > $this->entrylist[$this->ac]->type=T_STOPSET; > } else { > $this->entrylist[$this->ac]=new asxmisc($tstr); > $this->entrylist[$this->ac]->type=T_ENTRYREF; > } > $this->entrylist[$this->ac]->start=$lineno; > $this->entrylist[$this->ac]->end=$lineno; > $this->entrylist[$this->ac]->source=F_ORIG; > } else { > die("Invalid File"); > } > } >} >?> > >the callback.class.php has the following: > ><?php >class string_callback { > >var $callbacks; >var $parent; > >function string_callback () { > $this->callbacks = array("trigger" => "", > "function"=>"", "ordinal" >=>1); >} >/* >String Call back has the following properties: > callbacks = array of callback objects/arrays > callback["trigger"] = trigger regxep > callback["function"] = callback function > callback["ordinal"] = ordinal for execution order > > registerd functions shall have a single parameter, the portion > of the string that the regexp represents > > String Callback has the following methods: > register($trigger, $function [, $ordinal]) > register callback function $function to be called > when $trigger is found. Ordinal is for order of the > array of registered callback functions > > string($text, $lineno) > function to check string for registered regexps and > call callback functions. Functions will be called as > $function($match, $lineno) where match is the part of > the string that the regexp matched and lineno is the > ordinal line number. All functions must be complete > before the next regexp is checked. If ordinal is not > specified, then the function will register at the > current end of the array and will be assigned an > ordinal of max(ordinal)+1 >*/ > >function ordsort($a, $b) { > if ($a["ordinal"]==$b["ordinal"]) return 0; > return ($a["ordinal"] > $b["ordinal"]) ? 1 : -1; >} > >function register($trigger, $function, $ordinal=-1) { > if ($ordinal==-1) { > $arlen = sizeof($this->callbacks); > $maxord = $this->callbacks[$arlen-1]["ordinal"]; > $ordinal = $maxord + 1; > } > $push = array("trigger" => $trigger, "function" => $function, >"ordinal" => $ordinal); > array_push($this->callbacks,$push); > uasort($this->callbacks, $this->ordsort); > return TRUE; >} > >function string($text, $lineno=-1) { > $arlen = sizeof($this->callbacks); > for ($i=0;$i<$arlen;$i++) { > unset($retar); > if (!empty($this->callbacks[$i]["trigger"])) { > if (preg_match($this->callbacks[$i] >["trigger"],$text,$retar)) { > $this->callbacks[$i]["function"]($retar[0],$lineno); > } > } > } > return TRUE; >} >} >?> > > > >-- >PHP General Mailing List (http://www.php.net/) >To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net >For additional commands, e-mail: php-general-help@lists.php.net >To contact the list administrators, e-mail: php-list-admin@lists.php.net > > >

« previous php.general (#1692) next »