Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 675
  • Last Modified:

PHP OOP method variable scope resolution

Dear Experts - Is it possible to access the $food variable from outside its local function/method?  For example:

$keeper = new Zoo();

echo $keeper->horse::food; //Would return "hay"

echo $keeper->lion::food; //Would return "meat"

class Zoo {
  public function horse() {
    $food='hay';
  }
  public function lion(){
    $food='meat';
  }
}

I had been using $this->food but as I start getting more and more functions in the class sometimes I forget and have used the $this->food twice thus overwriting the earlier value by accident.  So I'm looking for a way to stay more organized.

Thanks,

http://www.t1shopper.com/
0
Geoff Millikan
Asked:
Geoff Millikan
  • 3
  • 3
  • 2
  • +1
3 Solutions
 
Beverley PortlockCommented:
Ideally you never access a class property / variable directly and global variables are a pain in any case.

Really you should organise this differently. Ideally, look for commonalities and organise them into a base class. For new versions you extend the base. For instance

class zoo {
     protected food;

     function setFood( $f ) {
          $this->food = $f;
     }

     function getFood() {
          return $this->food;
     }
}


Then you do this

class Horse extends zoo {
     
     function __construct() {
          parent::setFood("Hay");
     }
}

class Lion extends zoo {
     
     function __construct() {
          parent::setFood("zebras");
     }
}

Then everywhere you used Zoo you now use the appropriate animal

$keeper = new Horse();
echo $keeper->getFood();    // outputs "hay"

$keeper = new Lion();
echo $keeper->getFood();    // outputs "zebras"




0
 
hieloCommented:
>> $food='hay';
In your example, those are two different $food variables. They exist only within that method

>>I had been using $this->food but as I start getting more and more functions in the class sometimes I forget and have used the $this->food twice thus overwriting the earlier value by accident
You probably have a class variable:
class Zoo{
 private $food;
 function horse(){
  $this->food='hay';
 }
 function lion(){
  $this->lion='hay';
 }
}

in that case, $this->food in both horse and lion methods refer to private $food.
0
 
LordZardeckCommented:
Personally, I am very glad to see another OOP convert ;-). Now, as to your question.  It would be better to have a class for each animal, and store it in the zoo class. For example:

<?php
  class Zoo {
      var $animals = array();
  }
 
  class Horse {
      var $food = "hay";
  }
 
  class Lion {
      var $food = "meat";
  }
?>

Then you could add the animals like so:

<?php
    $zoo = new Zoo();
    $zoo->animals["horse"] = new Horse();
    $zoo->animals["lion"] = new Lion();
?>

And retrieve the food like this:

<?php
    $food = $zoo->animals["horse"]->food;
?>

And of course there could be better ways also, but there would be a good start. No, you can't access function variables in PHP.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Beverley PortlockCommented:
ANother thought. If you do not want to extend the classes as I have shown then extend the properties of your zoo class like this



class zoo {

     protected $animal;
     protected $food;


     function __construct( $a="none", $f="" ) {
          $this->animal = $a;
          $this->food = $f;
     }

     function setFood( $f ) {
          $this->food = $f;
     }

     function getFood() {
          return $this->food;
     }

     function setAnimal( $f ) {
          $this->animal = $f;
     }

     function getAnimal() {
          return $this->animal;
     }

     
}

Then no matter how many more animals you want to have you do not need to have any more code

$an1 = new Zoo("Horse", "Hay");
$an2 = new Zoo("Lion", "Zebras");
$an3 = new Zoo("Human", "Anything he can catch");

Then outputing is simple

echo $an3->getAnimal() . " eats " . $an3->getFood();

You can even process arrays of these

foreach( $zoo as $anAnimal )
      echo $animal->getAnimal() . " eats " . $animal->getFood();

You can change things individually

$an3->setFood("Fish and Chips");

etc. This is far more robust and flexible than adding new code for each new animal.

0
 
LordZardeckCommented:
Whoops. Those comments weren't there when I started :P
0
 
LordZardeckCommented:
bportlock, the problem with you last code is that he has to have a zoo for each animal
0
 
Beverley PortlockCommented:
LordZardeck - in the example given the two are fairly equivalent. There is always more than one way to skin a cat....

0
 
Geoff MillikanAuthor Commented:
Wow, this is the best tutorial on how classes and methods really work I've ever seen!  This is awesome, just give me a few minutes here to read though the solutions 10 more times.  
0
 
Geoff MillikanAuthor Commented:
Awesome, thanks for your time in writing the examples!!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now