Skip to content

Instantly share code, notes, and snippets.

@ShawnMcCool

ShawnMcCool/php-visibility

Last active Aug 29, 2015
Embed
What would you like to do?
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
You can’t perform that action at this time.