Last active
August 29, 2015 14:17
-
-
Save ShawnMcCool/96e751a67b686572376f to your computer and use it in GitHub Desktop.
PHP Visibility by Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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