@ 2017-07-09T23:25:11Z <?php
class Object {
//
protected $hasMany;
protected $theSignificativeProperties = ['nome', 'login'];
public function getHasMany() {
return $this->hasMany;
}
public function getSignificativeProperties() {
return $this->theSignificativeProperties;
}
public function validar() {
//
}
}
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($array) {
//
echo 'Usando o RelationalEntityManager. ';
echo 'Vou fazer JOIN com ';
print_r($array);
$query = 'SELECT';
return array('query'=>$query, 'data'=>$array);
}
}
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);
}
public function save() {
$formFields = array('name'=>'Marcelo', 'login'=>'marcelo'); // $_POST;
}
}
class AlunoController extends BaseController {
}
class DisciplinaController extends BaseController {
}
class BaseObjectService { // BaseObjectEngine
protected $em;
protected $model;
public function __construct($modelName, EntityManager $entityManager) {
$this->model = $modelName;
$this->em = $entityManager;
}
public function informa_os_has_many($classe, $chave) {
//
$tem = false;
$reflect = new ReflectionClass(ucfirst($classe));
$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 = $reflect->getMethod('getHasMany');
$c = ucfirst($classe);
$x = new $c();
$array = $hasManyMethod->invoke($x);
if ( in_array($chave, $array) ) {
$tem = true;
break;
}
}
}
return $tem;
}
public function view($args) {
//
//echo 'Argumentos: ';
//print_r($args);
//echo 'Classe em questão: ' . $this->model;
//echo ' -> Propriedades: ';
$singleJoin = array();
$doubleJoin = array();
$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();
$array = array();
foreach ($methods as $method) {
print $method->getName() . ", ";
if ( $method->getName() == 'getHasMany') {
echo 'achou o HASMANY de ' . $this->model ;
//print_r($reflect->getHasMany());
$hasManyMethod = $reflect->getMethod('getHasMany');
$c = ucfirst($this->model);
$x = new $c();
$array = $hasManyMethod->invoke($x);
print_r($array);
foreach($array as $filho=>$hasManyDoFilho) {
if ( $this->informa_os_has_many($hasManyDoFilho, $this->model) ) {
$doubleJoin[] = $filho;
} else $singleJoin[] = $filho;
}
}
}
echo ' --- ';
// var_dump($props);
$results = $this->em->find(array('single join' => $singleJoin, 'double join' => $doubleJoin));
$data['results'] = $results;
$this->render($this->model, 'view', $data);
}
public function render($controller, $action, $data) {
extract($data);
echo 'require ' . $controller . '/' . $action . 'view.php';
echo 'dados recebidos: ';
print_r($data);
}
}
// 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);
$disciplinaController = new DisciplinaController('disciplina');
$disciplinaController->view('PHP', 55);
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
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.28 , 8.2.0 - 8.2.18 , 8.3.0 - 8.3.4 , 8.3.6 Fatal error: Cannot use 'Object' as class name as it is reserved in /in/eFRQY on line 3
Process exited with code 255 . Output for 8.3.5 Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
Fatal error: Cannot use 'Object' as class name as it is reserved in /in/eFRQY on line 3
Process exited with code 255 . Output for 5.6.0 - 5.6.40 , 7.0.0 - 7.0.33 , 7.1.0 - 7.1.33 nome, login, hasMany, theSignificativeProperties, getHasMany, achou o HASMANY de alunoArray
(
[0] => post
[1] => disciplina
)
Warning: in_array() expects parameter 2 to be array, null given in /in/eFRQY on line 120
getSignificativeProperties, validar, --- Usando o RelationalEntityManager. Vou fazer JOIN com Array
(
[single join] => Array
(
[0] => 0
)
[double join] => Array
(
[0] => 1
)
)
require aluno/viewview.phpdados recebidos: Array
(
[results] => Array
(
[query] => SELECT
[data] => Array
(
[single join] => Array
(
[0] => 0
)
[double join] => Array
(
[0] => 1
)
)
)
)
hasMany, theSignificativeProperties, getHasMany, achou o HASMANY de disciplinaArray
(
[0] => aluno
)
getSignificativeProperties, validar, --- Usando o RelationalEntityManager. Vou fazer JOIN com Array
(
[single join] => Array
(
)
[double join] => Array
(
[0] => 0
)
)
require disciplina/viewview.phpdados recebidos: Array
(
[results] => Array
(
[query] => SELECT
[data] => Array
(
[single join] => Array
(
)
[double join] => Array
(
[0] => 0
)
)
)
)
preferences:dark mode live preview
218.66 ms | 406 KiB | 291 Q