Solved

strategy pattern: Undefined variable: Workers

Posted on 2014-02-01
7
300 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
  • 3
  • 3
7 Comments
 
LVL 34

Accepted Solution

by:
gr8gonzo earned 333 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 34

Assisted Solution

by:gr8gonzo
gr8gonzo earned 333 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 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 167 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
Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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 34

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

Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now