?
Solved

strategy pattern: Undefined variable: Workers

Posted on 2014-02-01
7
Medium Priority
?
319 Views
Last Modified: 2014-02-19
<?php
class Bob
{
   public function DoYourJob()
   {
     $H = new Hammer();
     $N = new Nails();
     $W = new WoodPlanks();
     $this->_buildDesk($H, $N, $W);     
   }

  private function _buildDesk($Hammer,$Nails,$WoodPlanks)
  {
      echo'<br>... code that builds a desk ...';
  }
}

class Jerry
{
   public function DoYourJob()
   {
     $C = new Car();
     $C->DriveTo("123 Main Street");
   }
}

class WorkForce
{
   public $Workers = array();
   public function GoToWork()
   {
      foreach($Workers as $Worker)
      {
         $Worker->DoYourJob();
      }
   }
}

$WF = new WorkForce();
$WF->Workers[] = new Bob();
$WF->Workers[] = new Jerry();
$WF->GoToWork();
?>

Open in new window


Notice: Undefined variable: Workers in C:\wamp\www\test\gStrategy.php on line 32

Warning: Invalid argument supplied for foreach() in C:\wamp\www\test\gStrategy.php on line 32
0
Comment
Question by:rgb192
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 35

Accepted Solution

by:
gr8gonzo earned 1332 total points
ID: 39826914
Oops. It should be foreach($this->Workers as $Worker). That's what I get for not testing my code samples. Sorry.

Also, a note - both Bob and Jerry are just example code that reference classes that don't exist (Car, Hammer, etc...). It was meant for illustration.
0
 
LVL 35

Assisted Solution

by:gr8gonzo
gr8gonzo earned 1332 total points
ID: 39826918
If you want to add the classes to make it work, then add this code to the bottom of the file:

class Hammer
{
}

class Nails
{
}

class WoodPlanks
{
}

class Car
{
  public function DriveTo($address)
  {
    echo "Okay, we're driving to {$address}";
  }
}
0
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 668 total points
ID: 39826923
I would have to bet that forgetting $this-> ranks as the most frequent PHP coding error.   I am certain that it is my most frequent PHP coding error!
0
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 

Author Comment

by:rgb192
ID: 39857956
<?php
class Bob
{
   public function DoYourJob()
   {
     $H = new Hammer();
     $N = new Nails();
     $W = new WoodPlanks();
     $this->_buildDesk($H, $N, $W);     
   }

  private function _buildDesk($Hammer,$Nails,$WoodPlanks)
  {
      echo'<br>... code that builds a desk ...';
  }
}

class Jerry
{
   public function DoYourJob()
   {
     $C = new Car();
     $C->DriveTo("123 Main Street");
   }
}

class WorkForce
{
   public $Workers = array();
   public function GoToWork()
   {
      foreach($this->Workers as $Worker)
      {
         $Worker->DoYourJob();
      }
   }
}





class Hammer
{
}

class Nails
{
}

class WoodPlanks
{
}

class Car
{
  public function DriveTo($address)
  {
    echo "Okay, we're driving to {$address}";
  }
}

$WF = new WorkForce();
$WF->Workers[] = new Bob();
$WF->Workers[] = new Jerry();
$WF->GoToWork();

Open in new window



output

... code that builds a desk ...Okay, we're driving to 123 Main Street


where is the strategy
I thought strategy was 'if statement'
how is code different 'if something' happens
0
 

Author Closing Comment

by:rgb192
ID: 39863604
thanks
0
 
LVL 35

Expert Comment

by:gr8gonzo
ID: 39870359
Sorry, I was on vacation. The strategy part of it isn't just the word"if" - strategy patterns just indicate conditional processing in general - that the SAME bit of code can be executed DIFFERENTLY based on the conditions and parameters.
0
 

Author Comment

by:rgb192
ID: 39872475
SAME bit of code can be executed DIFFERENTLY based on the conditions and parameters.

thanks for clarification
0

Featured Post

Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question