3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Object { // protected $hasMany; protected $theSignificativeProperties = ['nome', 'login']; public function getHasMany() { return $this->hasMany; } public function getSignificativeProperties() { return $this->theSignificativeProperties; } } class Aluno extends Object { private $nome; private $login; protected $hasMany = ['post', 'disciplina']; protected $theSignificativeProperties = ['nome', 'login']; /*public function getHasMany() { return $this->hasMany; }*/ /*public function getSignificativeProperties() { return $this->theSignificativeProperties; }*/ } class Post extends Object { // } class Disciplina extends Object { protected $hasMany = ['aluno']; } class EntityManager { // } class RelationalEntityManager extends EntityManager { public function find() { // echo 'Usando o RelationalEntityManager.'; } } class BaseController { protected $objectService; protected $model; public function __construct($modelName) { $this->model = $modelName; $this->objectService = new BaseObjectService($modelName, new RelationalEntityManager()); } public function view() { $args = func_get_args(); $this->objectService->view($args); } } class AlunoController extends BaseController { } class BaseObjectService { protected $em; protected $model; public function __construct($modelName, EntityManager $entityManager) { $this->model = $modelName; $this->em = $entityManager; } public function view($args) { // echo 'Argumentos: '; print_r($args); echo 'Classe em questão: ' . $this->model; echo ' -> Propriedades: '; $reflect = new ReflectionClass(ucfirst($this->model)); // $props = $reflect->getProperties(ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED); $props = $reflect->getProperties(); foreach ($props as $prop) { print $prop->getName() . ", "; } $methods = $reflect->getMethods(); foreach ($methods as $method) { print $method->getName() . ", "; if ( $method->getName() == 'getHasMany') { echo 'achou o HASMANY de ' . $this->model ; //print_r($reflect->getHasMany()); $hasManyMethod = $reflection->getMethod('getHasMany'); $array = $hasManyMethod->invoke(); print_r($array); echo ' --- '; } } // var_dump($props); $this->em->find(); } } echo 'Aluno: ' . class_exists('Aluno') . '<br>'; echo 'Teste: '; var_dump(class_exists('Teste')) . '\n'; $a = new Aluno(); print_r ( $a->getSignificativeProperties() ); $alunoController = new AlunoController('aluno'); $alunoController->view('marcelo', 'boi', 1);
Output for 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.27, 8.2.0 - 8.2.17, 8.3.0 - 8.3.4
Fatal error: Cannot use 'Object' as class name as it is reserved in /in/e8rV0 on line 3
Process exited with code 255.
Output for 7.0.5 - 7.0.20, 7.1.0 - 7.1.33
Aluno: 1<br>Teste: bool(false) Array ( [0] => nome [1] => login ) Argumentos: Array ( [0] => marcelo [1] => boi [2] => 1 ) Classe em questão: aluno -> Propriedades: nome, login, hasMany, theSignificativeProperties, getHasMany, achou o HASMANY de aluno Notice: Undefined variable: reflection in /in/e8rV0 on line 107 Fatal error: Uncaught Error: Call to a member function getMethod() on null in /in/e8rV0:107 Stack trace: #0 /in/e8rV0(68): BaseObjectService->view(Array) #1 /in/e8rV0(129): BaseController->view('marcelo', 'boi', 1) #2 {main} thrown in /in/e8rV0 on line 107
Process exited with code 255.
Output for 7.0.0 - 7.0.4
Aluno: 1<br>Teste: bool(false) Array ( [0] => nome [1] => login ) Argumentos: Array ( [0] => marcelo [1] => boi [2] => 1 ) Classe em questão: aluno -> Propriedades: nome, login, hasMany, theSignificativeProperties, getHasMany, achou o HASMANY de aluno Notice: Undefined variable: reflection in /in/e8rV0 on line 107 Fatal error: Uncaught Error: Call to a member function getMethod() on unknown in /in/e8rV0:107 Stack trace: #0 /in/e8rV0(68): BaseObjectService->view(Array) #1 /in/e8rV0(129): BaseController->view('marcelo', 'boi', 1) #2 {main} thrown in /in/e8rV0 on line 107
Process exited with code 255.

preferences:
175.46 ms | 402 KiB | 199 Q