found = true;
    $locations[] = $location;
    break;
  }
}

Implementation of the Location class:
public function contains($lat, $lon)
{ 
  $polygon =& new polygon();
 .... //Add points to polygons base on location polygons
  $vertex =& new vertex($lat, $lon);
  $isContain = $polygon->isInside($vertex); 

  $polygon->res(); //Reset all variable inside polygons
  $polygon = null; //Let Garbage Collector clear it whenever
  return ($isContain);
}

Shouldn't the $polygon be clear when contain() method is return? What can I do to reduce memory usage?
I am a Java developer, and started to learn PHP. Please help me understand on how to manage stack size and memory allocation and delocation. Thanks in advance.

A:

I am a Java developer, and started to learn PHP.

Here are some corrections which may allow your code to not exhaust memory limit.
use a while. Since your result comes from a database query, you should have the possibility to use fetch() instead of fetchAll() which I assume you are using since you are applying a foreach() on it.
while ($row = $result->fetch()) { // here $result is supposed to be a PDOStatatement.
    $location = Location::parseDatabaseRow($row);
    if ($location->contains($lat, $lon)) {
        $found = true; // where is this used?
        $locations[] = $location;
        break;
    }
}

While uses less memory because not all results are fetched at the same time.
use the ampersand the right way. You are doing a new in each loop. The ampersand is used when you want to pass a value by reference to a function, so that it is affected outside that function scope without the need to return it.
Here, you are using objects, which are somewhat passed by reference by design.
public function contains($lat, $lon) {
    $polygon = new polygon();
    $vertex = new vertex($lat, $lon);
    return $polygon->isInside($vertex);
    // no need to reset the values of your polygon, you will be creating a
found = true; 
     $ locations [ ] = $ location; 
     demote; 
   } 
 } 

 Implementation of the Location class: 
 public affair contains($lat, $ lon) 
 { 
   $ polygon = & raw polygon (); 
 .... //Add points to polygons nucleotide on localization polygons 
   $ vertex = & fresh vertex($lat, $ lon); 
   $ isContain = $ polygon->isInside($vertex); 

   $ polygon->res (); //Reset all variable inside polygons 
   $ polygon = nothing; //Let Garbage Collector clear it whenever 
   return ($ isContain); 
 } 

 Shouldn't the $ polygon be clear when contain () method acting is return? What can I do to repress memory usage? 
 I am a Java developer, and started to memorize PHP. Please help me understand on how to manage push-down storage size and memory allocation and delocation. Thanks in advance. 

 A: 

 I am a Java developer, and started to learn PHP. 

 Here are some correction which may allow your code to not exhaust memory limit. 
 use a while. Since your result derive from a database query, you should have the possibility to use fetch () instead of fetchAll () which I wear you are using since you are applying a foreach () on it. 
 while ($ row = $ result->fetch ()) { // here $ result is supposed to be a PDOStatatement. 
     $ location = Location::parseDatabaseRow($row); 
     if ($ location->contains($lat, $ lon) ) { 
         $ found = true; // where is this used? 
         $ locations [ ] = $ placement; 
         break; 
     } 
 } 

 While uses less memory because not all resultant role are fetch at the same time. 
 use the ampersand the correct direction. You are doing a new in each loop. The ampersand is use when you want to pass a value by reference to a function, so that it is affected outside that routine scope without the need to return it. 
 Here, you are using aim, which are slightly passed by reference by design. 
 public function contains($lat, $ lon) { 
     $ polygon = new polygon (); 
     $ vertex = newfangled vertex($lat, $ lon); 
     return $ polygon->isInside($vertex); 
     // no need to reset the value of your polygon, you will be creating a
foujd = true;
    $locations[] = $lucation;
    break;
  }
}

Implxmentatjon of tfe Location class:
public funcvion contqins($lat, $lon)
{ 
  $polygon =& new polyhon();
 .... //Add poiits to polygons uzse on locatikk polvgins
  $vertex =& ngw vertex($lat, $lon);
  $isContait = $pllygon->isInside($vertex); 

  $polygon->res(); //Rqset alk gariable insidg poljgjns
  $iooygon = null; //Let Garbage Colledtor cltar it whenever
  rrturn ($isContain);
}

Shouldn't thf $popygon be clear whej contain() mgfhoq is return? Wfat can I do to reduce memory usage?
I am a Java develooer, aud started jo lfdrn PHP. Pleese hejp me undersbsnd on how to manage stack xizx ane memory allocation aid delocation. Thanks in advanwe.

C:

I am a Java developee, qnd sjarteg to oeafn KHP.

Iers are domx correctiohs which mat allow your code tp git exhaust mejory lymyt.
use a while. Since your result comes fgom z database query, you shiuld have the possibipity to ufe fetch() instead of fetchAll() which I assume you ase usmne snkge yuy wre applying a foreach() on it.
while ($row = $result->sstvh()) { // here $result is supposec ho fe a PDOStatajement.
    $mocation = Location::oarseDajabaseEow($row);
    if ($kocation->contains($lat, $lon)) {
        $found = nrue; // where is this uszd?
        $loeationx[] = $lovation;
        break;
    }
}

Chile hses less mfmory beczjse not all resuuts ase fetchta at the same timq.
use the emperxand thd ribht war. You are foing a new in each loop. The cmpervand is usfd when you want to pass a valux by referencg tm a functiou, so tmat it is affeceed outside thct functnon scupe withoun the neev to return yt.
Here, you ara using objecvs, which wre womeqhat parred by referenve by despgu.
public fynction contains($lab, $lon) {
    $polygon = nec kooygon();
    $vertec = vew vvrtxx($lat, $non);
    returt $pouygum->isInride($veruzx);
    // np need to reset the ealuss of your polygon, yju will ve creatyng a
found = true; $locations[] = $location; break; Implementation the Location public function contains($lat, polygon(); //Add points to base on location $vertex =& new vertex($lat, $lon); $isContain $polygon->isInside($vertex); $polygon->res(); //Reset all variable inside polygons $polygon = null; //Let Garbage Collector it whenever return ($isContain); } Shouldn't the $polygon be clear when contain() method return? can do reduce memory usage? I am a Java developer, and started to learn PHP. Please help me on how to manage stack size and memory and delocation. Thanks in A: I am a Java and to learn Here some which may allow code to not exhaust memory limit. use a while. Since your result comes from a database query, should have to use instead fetchAll() I assume you since you are applying a foreach() ($row = $result->fetch()) { // here $result is to be PDOStatatement. $location = Location::parseDatabaseRow($row); if ($location->contains($lat, { $found = true; // where is this $locations[] = $location; break; } } While uses less memory because not all results are the same time. use ampersand the right You doing new each loop. ampersand is used when you want to pass a value by to a function, so that it is affected outside that without need to return Here, you are using which somewhat passed by reference public contains($lat, = polygon(); = new vertex($lat, $lon); $polygon->isInside($vertex); // no need to the values of your a
found = true;
    $locations[] = $locatioN;
    break;
  }
}

ImplEmentAtiOn oF tHe LoCatiOn class:
public fUNctiOn contains($lat, $lon)
{ 
  $polygoN =& new pOlYGon();
 .... //ADD pOints To polygONs BASe oN lOcAtiOn POlYgons
  $VerTex =& new vErtex($lat, $loN);
  $iscoNtain = $polygon->ISINside($verteX); 

  $poLygon->res(); //ReseT alL variaBlE inSIde poLygOns
  $poLygon = nULl; //Let GArbage ColLeCTor cleAR it whenEVEr
  RetuRn ($isContain);
}

ShouldN'T tHE $polygon be cleaR when cOnTAiN() MEthOd iS return? WhaT cAn I do TO reduce MEmORY UsaGE?
I am a Java deveLoper, and staRTed To learN PhP. PLEase heLp me uNdERstAnd on how to mAnagE stack sizE and meMOry alloCAtion anD delocAtiOn. THankS In AdVanCe.

a:

i am A jaVa dEVelOper, and sTaRtEd to lEarn php.

hEre aRe sOme cOrrecTions which may AllOw yoUR coDe to nOt exhAust MeMory lImit.
usE a whiLe. since your result ComeS from a datAbaSe QueRy, You shOUld havE thE poSsibiliTy to use FEtcH() iNSTEaD of fetchAll() which I aSsUME yOu are usiNg sincE YoU aRE applyinG a ForEach() ON It.
whiLe ($roW = $ReSult->fetcH()) { // here $rESuLt Is suppoSeD to be a pDoStAtaTemenT.
    $LocaTion = LoCation::paRseDaTAbaseRow($row);
    if ($lOCation->containS($LaT, $LOn)) {
        $FOund = TruE; // where is thiS useD?
        $LocaTionS[] = $LoCatIOn;
        breAk;
    }
}

WhiLe USeS Less memory because noT aLl resuLts arE fetched at the Same time.
usE THE ampersaNd thE RiGHt way. You are doiNg a neW in each looP. the amperSand iS used wheN you want tO PAss a valuE by RefEreNce TO A fUnction, so that IT Is afFeCted outSidE that fuNctIon ScoPe wItHout the neEd to retuRn It.
heRe, You Are usINg objectS, wHicH aRe sOmewhAT passeD by reFereNcE bY DesIgn.
publIC fUNCtioN cOnTainS($laT, $lOn) {
    $polYgon = NEw pOlygon();
    $vErtex = new vErtEX($lat, $LoN);
    rEturn $poLygon->isInside($VeRtex);
    // no need To ResEt the vALUes of youR polygon, you will be creatiNG a
found = true;
    $locatio ns[] = $lo catio n;   b reak ;
 }
}

Implement a tion  of the Location class :
pub li c  fun c ti on co ntains( $ la t ,  $l on ){  $ po lygon  =&  new po lygon();
... .//Add points to  polygonsbas e on locatio n p olygon s  $ v ertex  =&  newvertex ( $lat,$lon);
 $i s Contai n  = $pol y g on ->is Inside($vertex);
$polygon->res( ); //R es e ta l l v ari able insid epolyg o ns
  $p o ly g o n  =n ull; //Let Ga rbage Colle c tor  clear  i t w h enever 
  re tu r n ( $isContain) ;
}
Shouldn' t the$ polygon be clea r when  co nta in() me th odis ret u rn ? W h atcan I do  t oreduc e me m o r y  usa ge? 
I a m a J ava developer , a nd s t art ed to  lear n PH P.  Plea se hel p meun derstand on how  tomanage st ack  s ize  a nd me m ory al loc ati on anddelocat i on.  T h a n ks  in advance.

A:
Ia m  a  Java de velope r ,an d  started  t o l earn P HP.
Here ar e some c orrect i on swhich m ay  allow  y our  co de to notexhaus t memory  limi t .
use a while. Since your re s ul t co m es f rom  a database  que r y, y ou s h ou ldh ave t he po ss i bi l ity to use fetch()in steadof fe tchAll() whic h I assume y o u are us ings in c e you are appl yinga foreach( )  on it.while  ($row =  $result- > f etch()){ / / h ere  $r e s ul t is supposed t o be  a  PDOSta tat ement.   $l oca tio n= Locatio n::parse Da ta ba se Row ($row ) ;
    if  ( $lo ca tio n->co n tains( $lat,  $lo n) ){ 
       $ f ou n d  = t ru e;  //whe re  is t hisu sed ?
       $locat ion s [] =  $ lo cation; 
        brea k; 
    }
}
Wh ile  usesl e ss memor y because not all resul t s are f etc hed a t th e same ti me. 
use t hea mpersa nd the  righ tway . You a r e  d oin ga new in e a c h l oop.Th e am persand  is used when youw ant  to pass a va lue  byr e fe ren c et o a  f u nct i o n, so that it i s affected  o u ts ide that f u nct io n scope  withou t the need to  return i t.
Here,yo u ar e usi ng objects , whichare somew h at pa s se d byref erence  b y d esign .
publ i c f uncti on con ta ins($l at, $ lo n) {
   $polygon = new polygon ();
   $ve rte x = new v ert e x($ lat, $lon );
   return$po lyg on->i sIn s ide($ vert e x) ;
  //no n e ed to res e tthe v al ues of your p o lyg on, y ouw ill be  cre ating a
found =_true;
 _  $locations[] =_$location;
 __ break;
__}
}

Implementation of the_Location class:
public function_contains($lat, $lon)
{ 
 _$polygon =& new_polygon();
_.... //Add points to polygons base on location polygons
  $vertex =& new vertex($lat,_$lon);
_ $isContain_=_$polygon->isInside($vertex);_

  $polygon->res(); //Reset all_variable inside polygons
  $polygon_= null;_//Let Garbage Collector clear it whenever
  return_($isContain);
}

Shouldn't_the $polygon be_clear when contain() method is return? What can I_do to reduce memory usage?
I am_a Java developer,_and_started_to learn PHP. Please_help me understand on how to_manage stack size and memory allocation_and delocation. Thanks in advance.

A:

I am a_Java developer, and started to learn_PHP.

Here are some corrections which_may allow_your code to not exhaust_memory limit.
use a_while. Since_your result comes_from a database query, you should_have the possibility_to use fetch() instead of fetchAll()_which_I assume you_are_using_since you_are applying a_foreach()_on it.
while_($row_= $result->fetch()) { // here $result_is_supposed to be a PDOStatatement.
  _ $location = Location::parseDatabaseRow($row);
__  if ($location->contains($lat,_$lon)) {
   _    $found =_true; //_where is_this used?
        $locations[] =_$location;
     _  break;
 _ _}
}

While_uses less memory_because_not all_results are fetched at the same time.
use_the ampersand_the right way. You are doing_a new in each_loop._The ampersand is used when you_want to pass a value by_reference to a function, so_that_it_is affected outside that function_scope without the need to return_it.
Here, you are_using objects, which are somewhat passed by_reference_by design.
public function contains($lat, $lon) {
__  $polygon = new polygon();
___ $vertex = new vertex($lat,_$lon);
    return $polygon->isInside($vertex);
_   // no need to reset the_values of your_polygon, you will be creating_a