Solved

strategy pattern: Undefined variable: Workers

Posted on 2014-02-01
7
317 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 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 35

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 110

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

688 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