Skip to content

Instantly share code, notes, and snippets.

@ShawnMcCool
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ShawnMcCool/96e751a67b686572376f to your computer and use it in GitHub Desktop.
Save ShawnMcCool/96e751a67b686572376f to your computer and use it in GitHub Desktop.
PHP Visibility by Example
<?php
namespace PrivateExample {
class Animal {
private $innermostFeelings = "HUNGER";
public function __construct() {
echo "Animal feels $this->innermostFeelings\n";
}
public function empathy(Animal $other) {
echo "The other animal feels $other->innermostFeelings\n";
}
}
$animal = new Animal;
class Shawn extends Animal {
public function __construct() {
echo "Shawn feels $this->innermostFeelings\n";
}
}
$shawn = new Shawn;
$other = new Animal;
$other->empathy($animal);
}
namespace ProtectedExample {
class Animal {
protected $innermostFeelings = "HUNGER";
public function __construct() {
echo "Animal feels $this->innermostFeelings\n";
}
public function empathy(Animal $other) {
echo "The other animal feels $other->innermostFeelings\n";
}
}
$animal = new Animal;
class Shawn extends Animal {
public function __construct() {
echo "Shawn feels $this->innermostFeelings\n";
}
}
$shawn = new Shawn;
$other = new Animal;
$other->empathy($animal);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment