How to reuse static class properties when class are inherited in PHP

1 Answer

0 votes
class Car {
    protected static $brand = 'no brand';
    
    public static function brand() {
         return static::$brand . "<br />";
    }
}

class Geep extends Car {
    protected static $brand = 'Grand Cherokee Trackhawk';
}

class BMW extends Car {
    protected static $brand = 'BMW X6 M';
}

echo (new Car)->brand();
echo (new Geep)->brand();

$object = new BMW();
echo $object->brand();

 
/*
run: 
 
no brand
Grand Cherokee Trackhawk
BMW X6 M
  
*/ 

 



answered Sep 9, 2017 by avibootz

Related questions

2 answers 881 views
2 answers 223 views
223 views asked Sep 7, 2017 by avibootz
3 answers 292 views
2 answers 260 views
2 answers 266 views
...