Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Declaring class properties or methods as static makes them accessible without needing an instantiation of the class. A property declared as static cannot be accessed with an instantiated class object (though a static method can).
- <?php
- class Foo {
- public static function aStaticMethod() {
- // ...
- }
- }
- Foo::aStaticMethod();
- $classname = 'Foo';
- $classname::aStaticMethod();
- ?>
- ======================================================
- <?php
- class Foo
- {
- public static $my_static = 'foo';
- public function staticValue() {
- return self::$my_static;
- }
- }
- class Bar extends Foo
- {
- public function fooStatic() {
- return parent::$my_static;
- }
- }
- print Foo::$my_static . "\n";
- $foo = new Foo();
- print $foo->staticValue() . "\n";
- print $foo->my_static . "\n"; // Undefined "Property" my_static
- print $foo::$my_static . "\n";
- $classname = 'Foo';
- print $classname::$my_static . "\n";
- print Bar::$my_static . "\n";
- $bar = new Bar();
- print $bar->fooStatic() . "\n";
- ?>
- =============Outout of the above================
- foo
- foo
- Notice: Accessing static property Foo::$my_static as non static in /in/V0Rvv on line 23
- Warning: Undefined property: Foo::$my_static in /in/V0Rvv on line 23
- foo
- foo
- foo
- foo
- PHP has abstract classes and methods. Classes defined as abstract cannot be instantiated, and any class that contains at least one abstract method must also be abstract. Methods defined as abstract simply declare the method's signature; they cannot define the implementation.
- When inheriting from an abstract class, all methods marked abstract in the parent's class declaration must be defined by the child; additionally, these methods must be defined with the same (or a less restricted) visibility. For example, if the abstract method is defined as protected, the function implementation must be defined as either protected or public, but not private. Furthermore the signatures of the methods must match, i.e. the type hints and the number of required arguments must be the same. For example, if the child class defines an optional argument, where the abstract method's signature does not, there is no conflict in the signature.
- <?php
- abstract class AbstractClass
- {
- // Force Extending class to define this method
- abstract protected function getValue();
- abstract protected function prefixValue($prefix);
- // Common method
- public function printOut() {
- print $this->getValue() . "\n";
- }
- }
- class ConcreteClass1 extends AbstractClass
- {
- protected function getValue() {
- return "ConcreteClass1";
- }
- public function prefixValue($prefix) {
- return "{$prefix}ConcreteClass1";
- }
- }
- class ConcreteClass2 extends AbstractClass
- {
- public function getValue() {
- return "ConcreteClass2";
- }
- public function prefixValue($prefix) {
- return "{$prefix}ConcreteClass2";
- }
- }
- $class1 = new ConcreteClass1;
- $class1->printOut();
- echo $class1->prefixValue('FOO_') ."\n";
- $class2 = new ConcreteClass2;
- $class2->printOut();
- echo $class2->prefixValue('FOO_') ."\n";
- ?>
- The above example will output:
- ConcreteClass1
- FOO_ConcreteClass1
- ConcreteClass2
- FOO_ConcreteClass2
- ================================================================
- Object interfaces allow you to create code which specifies which methods a class must implement, without having to define how these methods are implemented.
- Interfaces are defined in the same way as a class, but with the interface keyword replacing the class keyword and without any of the methods having their contents defined.
- All methods declared in an interface must be public; this is the nature of an interface.
- To implement an interface, the implements operator is used. All methods in the interface must be implemented within a class; failure to do so will result in a fatal error. Classes may implement more than one interface if desired by separating each interface with a comma.
- ============================================================
- <?php
- class Base {
- public function sayHello() {
- echo 'Hello ';
- }
- }
- trait SayWorld {
- public function sayHello() {
- parent::sayHello();
- echo 'World!';
- }
- }
- class MyHelloWorld extends Base {
- use SayWorld;
- }
- $o = new MyHelloWorld();
- $o->sayHello();
- ?>
- The above example will output:
- Hello World!
- ========================================================
- <?php
- trait HelloWorld {
- public function sayHello() {
- echo 'Hello World!';
- }
- }
- class TheWorldIsNotEnough {
- use HelloWorld;
- public function sayHello() {
- echo 'Hello Universe!';
- }
- }
- $o = new TheWorldIsNotEnough();
- $o->sayHello();
- ?>
- The above example will output:
- Hello Universe!
- ===========================================================================
- Final Keyword ΒΆ
- The final keyword prevents child classes from overriding a method by prefixing the definition with final. If the class itself is being defined final then it cannot be extended.
- Example #1 Final methods example
- <?php
- class BaseClass {
- public function test() {
- echo "BaseClass::test() called\n";
- }
- final public function moreTesting() {
- echo "BaseClass::moreTesting() called\n";
- }
- }
- class ChildClass extends BaseClass {
- public function moreTesting() {
- echo "ChildClass::moreTesting() called\n";
- }
- }
- // Results in Fatal error: Cannot override final method BaseClass::moreTesting()
- ?>
- Example #2 Final class example
- <?php
- final class BaseClass {
- public function test() {
- echo "BaseClass::test() called\n";
- }
- // Here it doesn't matter if you specify the function as final or not
- final public function moreTesting() {
- echo "BaseClass::moreTesting() called\n";
- }
- }
- class ChildClass extends BaseClass {
- }
- // Results in Fatal error: Class ChildClass may not inherit from final class (BaseClass)
- ?>
Add Comment
Please, Sign In to add comment