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 897 views
2 answers 230 views
230 views asked Sep 7, 2017 by avibootz
3 answers 299 views
2 answers 270 views
2 answers 276 views
...