Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class People{ //blueprint, patern, object factory
- protected $skils;
- // it's belongs only to the class
- static $workingHours = 8;
- function __construct($skils) {
- $this->skils = $skils;
- //same as top
- // $this->setSkils($skils);
- // how to work with static variables
- self::$workingHours --;
- }
- protected function setSkils($value)
- {
- $this->skils = $value;
- }
- /*
- * accesors can be: public, private, static, protected
- */
- //accesors and parameters
- public $fname;
- public $age;
- private $email = "@gmail.com";
- protected $sex = "male";
- // getter
- function getSex() {
- return $this->sex;
- }
- // setter
- function setSex($sex) {
- $this->sex = $sex;
- }
- function toString() // by default methods == functions are public
- {
- return "My info | Name: " . $this->fname . " Age: " . $this->age . "<br>";
- }
- // reference method
- function moreInfo()
- {
- return "<strong> Hey ... " . $this->toString() . "</strong>";
- }
- function returnSex() {
- return "Sex: " . $this->sex . "<br>";
- }
- // globa getter variable
- function getProperty($name)
- {
- return $this->$name;
- }
- // global setter variable
- function setProperty($name, $value)
- {
- $this->$name = $value;
- }
- // magic build in methods
- function __set($name, $value) {
- return "The property of: " . $name . "can not be set with value: ". $value;
- }
- // magic build in methods
- function __get($name) {
- return "The propert can not be found with name: " . $name;
- }
- protected function ddv()
- {
- return 0.18;
- }
- // only working with static properties, static methods or protected
- static function incrementWorkingHours($value) {
- $local_var = ((self::$workingHours+= $value)/100) * self::ddv();
- return $local_var;
- }
- static function salaryPerHour($value) {
- return self::incrementWorkingHours($value) * 2;
- }
- function __call($method, $args) {
- echo "<br>The method: " .$method . " does not exist.";
- foreach ($args as $value)
- {
- echo "<br>" . $value . " does not exist";
- }
- }
- function __toString() {
- return "<br>" . $this->fname .", " . $this->age . ", " . $this->sex . "," . $this->skils;
- }
- function __destruct() {
- //echo "<br>Destructor is here";
- }
- }
- // final set not inheritable class, you can put it final methods
- //so that methods can't be callable in this class from the parent class
- final class IT_engineer extends People {
- function __construct($skils) {
- $this->fname = "Mitko";
- parent::__construct($skils); //you overwrite the __constract function
- }
- //overwrite method
- function returnSex() {
- //calling the parent method
- //parent::returnSex();
- return "Fe/male sex:" . $this->sex . "<br> PARENT:" . parent::returnSex();
- }
- }
- abstract class Viacle{
- protected $cost;
- protected $weight;
- const REGISTRATION = "MKD";
- function __construct($c,$w) {
- $this->cost = $c;
- $this->weight = $w;
- }
- abstract function toString();
- }
- // interfecaes have only constants
- interface carInfo
- {
- const NUMBER = 2;
- function height($numPeople);
- }
- trait CarWindows {
- protected $typeOfGlass;
- function costTypeOfGlass($value) {
- $this->typeOfGlass = $value;
- }
- }
- class Car extends Viacle implements carInfo {
- //to add trait to class
- use CarWindows;
- protected $color;
- function __construct($color, $cost, $weight) {
- $this->color = $color;
- parent::__construct($cost, $weight);
- }
- public function toString()
- {
- echo "<br> CAR INFO: <br> COLOR: " . $this->color . ", COST: " . $this->cost . ", WEIGHT: " . $this->weight;
- }
- public function height($numPeople)
- {
- return 2*$numPeople;
- }
- }
- // instances
- $p = new People("PHP");
- $p->fname = "Andrej";
- $p->age = 23;
- echo $p->toString();
- echo $p->moreInfo();
- echo $p->returnSex();
- $p->setSex("female");
- echo $p->getSex();
- echo "<br>" . $p->getProperty("fname1");
- echo "<br>" . $p->getProperty("age");
- $p->setProperty("fname", "Petkana");
- echo "<br>" . $p->toString();
- // new instance decrement the static property
- $p1 = new People("Python");
- echo $p1->getProperty("skils");
- //unset($p1);
- echo "WORKING HOURS: " . People::$workingHours;
- echo "<br> WORKING HOURS: ". People::incrementWorkingHours(3);
- echo "<br> Incrised Salary per Workers: " . People::salaryPerHour(1) . "$";
- // for example does not exist.
- echo $p->aaa('aaa',123,'ggg');
- echo $p;
- echo "<br> INSTANCEOF: " . $p instanceof People;
- // predifined functions
- //var_dump(get_declared_classes());
- //var_dump(class_exists("People"));
- //var_dump(property_exists($p, "fname"));
- //var_dump(get_class_methods($p));
- //Inheritance
- $e = new IT_engineer("C++");
- //echo "THE NAME IS: " . $e->getProperty("email");
- //echo $e::$workingHours;
- echo "<br>" . $e->returnSex();
- echo $e->getProperty('skils');
- $c = new Car("red",200,6000);
- $c->toString();
- echo "REGISTRATION" . Viacle::REGISTRATION;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement