shamuntoha Posted October 4, 2008 Share Posted October 4, 2008 How to find John Smith? ??? <? /* * This is a test of Class and there uses * Thanks for the NuSphere PHPed * */ // A Town of Usa class town{ // A building, with 3rd floor function f_building1($id){ switch($id){ case 1: echo "Karnel Smith, eating <br>"; return 0; break; case 2: echo "Peter Smith, showering <br>"; return 0; break; case 3: echo "John Smith, hacking.. <br>"; return "John Smith"; break; } } // A building, with 2 floor function f_building2($id){ switch($id){ case 1: echo "Bush Smith, reading reports <br>"; return 0; break; case 2: echo "Jack Smith, no job.. smoking.. <br>"; return 0; break; } } } // Police inquery, from car $pol = new town(); // Searching all building, Find John Smith!! programmer... $searching = "John Smith"; for($i=0;$i<10;$i++){ $moving = $pol->f_building1($i); if ( $moving==$searching ) { echo "Sir, we found him, arrest him. <br>"; }else if($pol->f_building2($i)==$searching ){ echo "Sir, we found him, arrest him. <br>"; } } ?> Link to comment https://forums.phpfreaks.com/topic/127016-if-fix/ Share on other sites More sharing options...
Barand Posted October 4, 2008 Share Posted October 4, 2008 Well, not by comparing "John Smith" with a function that always returns 0 Link to comment https://forums.phpfreaks.com/topic/127016-if-fix/#findComment-657143 Share on other sites More sharing options...
shamuntoha Posted October 4, 2008 Author Share Posted October 4, 2008 its ok now, strcmp() was needed to use, instead of using if ( ). <?php /* * This is a test of Class and there uses * Thanks for the NuSphere PHPed * */ // A Town of Usa class town{ // A building, with 3rd floor function f_building1($id){ switch($id){ case 1: echo "Karnel Smith, eating <br>"; return 0; break; case 2: echo "Peter Smith, showering <br>"; return 0; break; case 3: echo "John Smith, hacking.. <br>"; return "John Smith"; break; } } // A building, with 2 floor function f_building2($id){ switch($id){ case 1: echo "Bush Smith, reading reports <br>"; return 0; break; case 2: echo "Jack Smith, no job.. smoking.. <br>"; return 0; break; } } } // Police inquery, from car $pol = new town(); // Searching all building, Find John Smith!! programmer... $searching = "John Smith"; for ($i=0; $i<4; $i++ ){ $moving1 = $pol->f_building1($i); $moving2 = $pol->f_building2($i); if ( strcmp($moving1,$searching)==0) { echo "Sir, we found him, arrest him. <br>"; }else if( strcmp($moving2,$searching)==0 ){ echo "Sir, we found him, arrest him. <br>"; } } ?> Link to comment https://forums.phpfreaks.com/topic/127016-if-fix/#findComment-657162 Share on other sites More sharing options...
Barand Posted October 4, 2008 Share Posted October 4, 2008 This is how I'd approach it <?php class town { private $buildings; public function __construct() { $this->buildings = array(); } public function addBuilding($building) { $this->buildings[] = $building; } public function search($name) { foreach ($this->buildings as $b) if ($location = $b->search($name)) { echo $b->getName(), '<br/>'; vprintf( "Found %s, %s on floor %d", $location); return; } echo "$name got away"; } } # town class building { private $residents; private $name; public function __construct($name) { $this->name = $name; $this->residents = array(); } public function addResident ($name, $activity, $floor) { $this->residents[] = array ( 'name' => $name, 'activity' => $activity, 'floor' => $floor ); } public function getName() { return $this->name; } public function search ($name) { foreach ($this->residents as $r) { if ($r['name'] == $name) return $r; } return false; } } # building /****** * populate the town */ $myTown = new town(); $b1 = new building('Trump Tower'); $b1->addResident('Karnel Smith', 'eating', 1); $b1->addResident('Peter Smith', 'showering', 2); $b1->addResident('John Smith', 'hacking', 3); $myTown->addBuilding($b1); $b2 = new building('Barand apartments'); $b2->addResident('Bush Smith', 'reading reports', 1); $b2->addResident('Jack Smith', 'no job.. smoking', 2); $myTown->addBuilding($b2); /******* * Search the town */ $searchFor = 'John Smith'; $myTown->search($searchFor); ?> Link to comment https://forums.phpfreaks.com/topic/127016-if-fix/#findComment-657175 Share on other sites More sharing options...
shamuntoha Posted October 4, 2008 Author Share Posted October 4, 2008 Thanks, it deep helped to understand PHP class tree. Link to comment https://forums.phpfreaks.com/topic/127016-if-fix/#findComment-657186 Share on other sites More sharing options...
nrg_alpha Posted October 4, 2008 Share Posted October 4, 2008 Barand, I have one question with regards to the key word 'public'. If I understand correctly, any method or property is by default public in nature with PHP, no? (unless someone declares otherwise [protected or private]) If so, is there an actual benefit to declaring things public? Is it more for programmer readability? Or is there more to it than that? Cheers, NRG Link to comment https://forums.phpfreaks.com/topic/127016-if-fix/#findComment-657250 Share on other sites More sharing options...
Barand Posted October 4, 2008 Share Posted October 4, 2008 Just being pedantic and explicitly stating that these methods do need to be public. But you're right, if I were writing it for myself I probably wouldn't bother. I also would've created a resident class, instead of just an array, but I didn't want to over-complicate things. My main concern was showing how to perform a search that didn't work only for a single pre-coded value. Link to comment https://forums.phpfreaks.com/topic/127016-if-fix/#findComment-657256 Share on other sites More sharing options...
nrg_alpha Posted October 4, 2008 Share Posted October 4, 2008 Ok, thanks for the response. (was just curious and 'covering all my bases' so-to-speak.. just in case I was missing something). Cheers, NRG Link to comment https://forums.phpfreaks.com/topic/127016-if-fix/#findComment-657267 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.