Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Define MyClass
- */
- class MyClass
- {
- // Declare a public constant
- public const MY_PUBLIC = 'public';
- // declare a protected constant
- protected const MY_PROTECTED = 'protected';
- // declare a private constant
- private const MY_PRIVATE = 'private';
- function foo()
- {
- echo self::MY_PUBLIC;
- echo self::MY_PROTECTED;
- echo self::MY_PRIVATE;
- }
- }
- $myclass = new MyClass();
- MyClass::MY_PUBLIC; // works
- MyClass::MY_PROTECTED; // fatal error
- MyClass::MY_PRIVATE; // fatal error
- $myclass->foo(); // public, protected and private work
- /**
- * define MyClass2
- */
- class MyClass2 extends MyClass
- {
- // this is public
- function foo2()
- {
- echo self::MY_PUBLIC;
- echo self::MY_PROTECTED;
- echo self::MY_PRIVATE; // fatal error
- }
- }
- $myclass2 = new MyClass2;
- echo MyClass2::MY_PUBLIC; // works
- $myclass2->foo2(); // public and protected work, not private
- ?>
Advertisement
Add Comment
Please, Sign In to add comment