Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

strategy pattern: Undefined variable: Workers

Posted on 2014-02-01
7
Medium Priority
?
321 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
Independent Software Vendors: 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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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 …
Suggested Courses

610 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