Skip to content

Instantly share code, notes, and snippets.

@alegut
Created February 3, 2018 20:40
Show Gist options
  • Save alegut/f1d19d982aeb5dc87f536dfabc3226c4 to your computer and use it in GitHub Desktop.
Save alegut/f1d19d982aeb5dc87f536dfabc3226c4 to your computer and use it in GitHub Desktop.
Object oriented programmong examples
<?php
//Class ShoppingCard {
//
//}
//
//$product1 = new ShoppingCard();
//$product2 = new ShoppingCard();
//$product3 = new ShoppingCard();
//
//var_dump($product1 instanceof ShoppingCard);
//var_dump($product2 instanceof Card);
//
//echo "<br><br><br>";
//class Shop {
// private $name;
//
// public function naming() {
// $this -> name = 'Adidas';
// echo $this -> name;
// }
//
//}
//
//$product = new Shop;
//$product->naming();
//
//echo "<br><br><br>";
//class User {
// public $name = 'Имя';
// public $password = 'Пароль';
// public $email = 'Мыло';
// public $city = 'Город';
//
// public function Hello(){
// echo "Hello! {$this->name}";
// }
//
// function __construct($name, $password, $email, $city){
// $this->name = $name;
// $this->password = $password;
// $this->email = $email;
// $this->city = $city;
// }
// function getInfo(){
// return "{$this->name}"."{$this->password}"."{$this->email}"."{$this->city}";
// }
//}
//$user1 = new User('Alex', '123456', 'alegut2005@ukr.net', 'Zaporizhzhya');
//echo "<br>".$user1->getInfo();
//$admin = new User();
//$admin->name = "Andrey";
//$admin->surname = "Ivanov";
//echo "Пользователь: {$admin -> getInfo()}"
//$user1 = new User();
//echo $user1->name="Andrey";
//$user1->surname="Ivanov";
//echo $user1->surname;
//class User {
// private static $name;
//
// public static function setName($name1){
// self::$name = $name1;
// }
// public static function getName(){
// return self::$name;
// }
//}
//User::setName('Ivan');
//echo User::getName()."<br>";
//
//class User {
// public $name;
// public $password;
// public $email;
// public $city;
//
// function __construct($name, $password, $email, $city){
// $this->name = $name;
// $this->password = $password;
// $this->email = $email;
// $this->city = $city;
// }
// function getInfo(){
// $information = "{$this->name}"."{$this->password}"."{$this->email}"."{$this->city}";
// return $information;
// }
//}
//
//class Moderator extends User {
// public $info;
// public $rights;
//
// function __construct($name, $password, $email, $city, $info, $rights){
// parent::__construct($name, $password, $email, $city);
// $this->info = $info;
// $this->rights = $rights;
// }
// function getInfo(){
// $information = parent::getInfo();
// $information .= "{$this->info}"."{$this->rights}";
// return $information;
// }
//}
//
//$mod = new Moderator('Alex', '123456', 'alegut2005@ukr.net', 'Zaporizhzhya', 'Moderator', 'True');
//echo $mod->getInfo().'<br>';
//
//class Test {
// protected $info;
//}
//
//class Test2 extends Test {
// public function test(){
// $this->info = 'info';
// echo $this->info;
// }
//}
//$test2 = new Test2;
//$test2->test();
//$test2->info = 'information'; //нет доступа к protected
// статические методы
//class user{
// public static $name;
//
// public static function hello() {
// echo "Hello ";
// return self::$name;
// }
//}
//
//user::$name="Alexey";
//echo user::$name;
//echo user::hello();
// Константы
//class user{
// const SOME_CONST = 314;
//}
//
//echo user::SOME_CONST;
// Абстрактные классы
//abstract class User {
// public $name;
// public $status;
//
// abstract public function getStatus();
//}
//
//class Admin extends User {
// public function getStatus(){
// echo "Admin";
// }
//}
////$user1 = new User; //вернет FATAL_ERROR -нельзя создать объект абстрактного сласса
//$user1 = new Admin;
//$user1->getStatus();
// Интерфейсы
//interface FirstInterFace {
// public function getName();
//}
//interface SecondInterFace {
// public function getStatus();
//}
//
//interface ThirdInterFace extends FirstInterFace, SecondInterFace{
//
//}
//
//class Test implements FirstInterFace, SecondInterFace {
// public $name = 'Alexey';
// public $status = 'Admin';
// public function getName(){
// echo $this->name;
// }
// public function getStatus(){
// echo $this->status;
// }
//}
//$user1 = new Test;
//echo $user1->getName();
//echo $user1->getStatus();
// Трейты
//class Base {
// public function sayHello(){
// echo "Hello ";
// }
//}
//trait sayWorld {
// public function sayHello(){
// parent::sayHello();
// echo "World";
// }
//}
//class myHelloWorld extends Base {
// use sayWorld;
//}
//
//$obj = new myHelloWorld();
//$obj -> sayHello();
//или так
//trait Hello {
// public function sayHello(){
// echo "Hello ";
// }
//}
//trait World {
// public function sayWorld(){
// echo "World";
// }
//}
//class myHelloWorld {
// use Hello, World;
//}
//
//$obj = new myHelloWorld();
//$obj -> sayHello();
//$obj -> sayWorld();
// Магический метод clone
//class User {
// private $name;
// private $city;
// private $id;
//
// function __construct($name, $city) {
// $this->name = $name;
// $this->city = $city;
// }
// function setId($id){
// $this->id = $id;
// }
//
// public function __clone() {
// $this->id = 0;
// }
//}
//
//$user1 = new User('Alexey', 'Zaporizhzhya');
//$user1->setId(5432);
//$user2 = clone $user1;
//var_dump($user2);
// Магические методы set, get
//class Getset {
// private $number =1;
//
// public function __get($name) {
// echo "You get {$name}";
// }
// public function __set($name, $value) {
// echo "You set {$name} to ";
// }
//}
//$obj = new GetSet();
//echo $obj->number;
//echo $obj->number = 68;
// Исключения
$file = 'namespace.php';
try{
if(!file_exists($file)) {
throw new Exception('Flie not found');
}
}catch (Exception $e){
echo $e->getMessage();
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment