php definovat třídu
class Bike {
function Bike() {
$this->type = 'BMX';
}
}
$blackSheep = new Bike();
print $blackSheep->type;
třída php
<?php
class Foo {
public $aMemberVar = 'aMemberVar Member Variable';
public $aFuncName = 'aMemberFunc';
function aMemberFunc() {
print 'Inside `aMemberFunc()`';
}
}
$foo = new Foo;
function getVarName()
{
return 'aFuncName';
}
print $foo->{$foo->{getVarName()}}();
oops koncepty v php
The PHP Object-Oriented Programming concepts are:
Class
Objects
Inheritance
Interface
Abstraction
Magic Methods
php vytvořit objekt
$x = (object) [
'a' => 'test',
'b' => 'test2',
'c' => 'test3'
];
var_dump($x);
/*
object(stdClass)#1 (3) {
["a"]=>
string(4) "test"
["b"]=>
string(5) "test2"
["c"]=>
string(5) "test3"
}
*/
vytvořte třídu v php
<?php
class Fruit {
public $name;
public $color;
function __construct($name, $color) {
$this->name = $name;
$this->color = $color;
}
function get_name() {
return $this->name;
}
function get_color() {
return $this->color;
}
}
$apple = new Fruit("Apple", "red");
echo $apple->get_name();
echo "<br>";
echo $apple->get_color();
?>
objekt php
$o= new \stdClass();
$o->a = 'new object';
OR
$o = (object) ['a' => 'new object'];