php_engine/archive/oop.php
2025-01-20 19:54:53 +03:00

87 lines
1.9 KiB
PHP

<?php
// классы именуем в Паскаль кейсе
class SectionTree {
static $radius = 10;
public $hight = 50; // в см
public function __construct( int $r, int $h ){
$this->radius = $r;
$this->hight = $h;
}
}
class SectionTreeChild extends SectionTree {
public $wight = 50;
public function __construct(){
parent::__construct(233, 23);
}
public function getRadius() {
return parent::$radius;
}
}
// $obj = new SectionTreeChild();
// var_dump($obj->getRadius());
// var_dump(SectionTree::$radius);
class ChristmasTree {
private $box = [];
protected $color = "red";
public function getColor(){
return $this->color;
}
public function addSection( SectionTree $section) {
array_push($this->box, $section);
}
public function debug() {
var_dump($this->box);
}
public function newSection(int $r, int $h) {
return new SectionTree($r, $h );
}
}
// () - вызов конструктора
// $tree = new ChristmasTree();
// $s_1 = $tree->newSection(100, 20);
// $s_2 = $tree->newSection(100, 20);
// $tree->addSection($s_1);
// $tree->addSection($s_2);
// $tree->debug();
class DB {
const SQL_SELECT_USER = 'select id, name, login
from user
where id = %';
static private $_instance;
public function __construct(){}
static public function getInstance() {
if (!self::$_instance) {
self::$_instance = new self();
}
return self::$_instance;
}
const SQL_SELECT_USE = 'select id, name
from user
where id = %';
static public function runSQL(){
var_dump(self::SQL_SELECT_USER);
}
}
$obj1 = new DB();
$obj2 = DB::getInstance();
$obj3 = DB::getInstance();
DB::runSQL();
var_dump($obj1 === $obj2);