Access Property of class in parent namespace - php

I have two class, main class is app.php in root directory, and db.php in \system
How To Get property $config in class base, with namespace pattern??
I want to get $config in class base, this is what I want
I define config for hostname,user,pass
then I declare base class wit new \App\base
I can get config in class db
<?php
// \App.php
namespace App;
class base{
private $config;
private $db;
function __construct($config){
$this->config = $config;
$this->db = new \App\system\db;
}
public function getTest() {
return $this->test;
}
}
function load($namespace) {
$splitpath = explode('\\', $namespace);
$path = '';
$name = '';
$firstword = true;
for ($i = 0; $i < count($splitpath); $i++) {
if ($splitpath[$i] && !$firstword) {
if ($i == count($splitpath) - 1) {
$name = $splitpath[$i];
} else {
$path .= DIRECTORY_SEPARATOR . $splitpath[$i];
}
}
if ($splitpath[$i] && $firstword) {
if ($splitpath[$i] != __NAMESPACE__) {
break;
}
$firstword = false;
}
}
if (!$firstword) {
$fullpath = __DIR__ . $path . DIRECTORY_SEPARATOR . $name . '.php';
return include_once ($fullpath);
}
return false;
}
function loadPath($absPath) {
return include_once ($absPath);
}
spl_autoload_register(__NAMESPACE__ . '\load');
?>
<?php
// \System\db.php
namespace App\system;
class db{
private $config;
function __construct(){
$this->config = "How To Get property $config in class base, with namespace pattern??";
}
}
?>

I would suggest you something like this:
class db{
private $test;
function __construct(){
include('../app.php');
$app = new base();
$this->test = $app->getTest();
}
}

Well, just pass the config when you create the db object. Also use the use keyword for better readability.
\App
namespace App;
use \App\system\db;
class base {
private $config;
private $db;
function __construct($config){
$this->config = $config;
$this->db = new db($config);
}
public function getTest() {
return $this->test;
}
}
\System\db.php
namespace App\system;
class db {
private $config;
function __construct($config){
$this->config = $config;
}
}

Related

how to factoring/group php methods

I've got a PHP class which contains different methods:
namespace App\Controllers;
class SuperAdminController extends Controller {
public function name1Action($wanted = ''){
$o = new name1Controller();
self::routeWanted($wanted,$o,$this);
}
...
public function name10Action($wanted = ''){
$o = new name10Controller();
self::routeWanted($wanted,$o,$this);
}
private function routeWanted($wanted,$o,$that){
switch($wanted){
do something...
}
}
}
How can I group all my public function as one function like
public function name1Action ... name10Action($wanted = ''){
$o = new name1Controller();
self::routeWanted($wanted,$o,$this);
}
You probably want __call Magic.
class SuperAdminController extends Controller {
public function __call($name, $args){
// list of method names
$mNames = [
'name1Action' => 1,
'name2Action' => 2,
'name3Action' => 3,
/* ... */
];
if (isset($mNames[$name])) {
$o = new {$name}();
return $this->nameAction($args[0], $o);
}
}
protected function nameAction($wanted = '', $o){
self::routeWanted($wanted,$o,$this);
}
}
You could use variable variables to solve this:
function Action($controller, $wanted = '') {
$c = "{$controller}Controller";
$o = new $c();
// ...
}
then you could use:
$s = new SuperAdminController();
$s->Action('name1');
Demo on 3v4l.org
change your code like the below:
namespace App\Controllers;
class SuperAdminController extends Controller {
// public function name1Action($wanted = ''){
// $o = new name1Controller();
// self::routeWanted($wanted,$o,$this);
// }
...
// public function name10Action($wanted = ''){
// $o = new name10Controller();
// self::routeWanted($wanted,$o,$this);
// }
public function beforeRouteWanted($wanted, $number) {
$class = 'name' . $number . 'Controller';
$o = new $class();
self::routeWanted($wanted, $o, $this);
}
private function routeWanted($wanted,$o,$that){
switch($wanted){
do something...
}
}
}

Class autoload not working

I'm a beginner in PHP development and I'm facing a problem in my development in PHP OO. I saw is better use the autoload() function than include each file of PHP Class.
My doubt is: Why my autoload function does not work?
Follow bellow my code:
<?php
function __autoload($class)
{
include_once "model/{$class}.class.php";
}
$avaliacaoLocal = new AvaliacaoLocal();
$avaliacaoLocal->setId(1);
$avaliacaoLocal->setIdLocal(2);
$avaliacaoLocal->setComentarios("Comentários de Pedro");
$avaliacaoLocal->setIdPessoaCliente(3);
$avaliacaoLocal->setValor(5);
var_dump($avaliacaoLocal);
File AvaliacaoLocal.class.php
<?php
namespace model;
class AvaliacaoLocal
{
private $id;
private $valor;
private $comentarios;
private $idLocal;
private $idPessoaCliente;
public function __construct(){
$this->clear();
}
public function clear(){
$this->id = 0;
$this->valor = 0;
$this->comentarios = "";
$this->idLocal = null;
$this->idPessoaCliente = null;
}
public function getId()
{
return $this->id;
}
public function setId($id)
{
$this->id = $id;
}
public function getValor()
{
return $this->valor;
}
public function setValor($valor)
{
$this->valor = $valor;
}
public function getComentarios()
{
return $this->comentarios;
}
public function setComentarios($comentarios)
{
$this->comentarios = $comentarios;
}
public function getIdLocal()
{
return $this->idLocal;
}
public function setIdLocal($idLocal)
{
$this->idLocal = $idLocal;
}
public function getIdPessoaCliente()
{
return $this->idPessoaCliente;
}
public function setIdPessoaCliente($idPessoaCliente)
{
$this->idPessoaCliente = $idPessoaCliente;
}
}
The error:
PHP Fatal error: Class 'AvaliacaoLocal' not found in C:\Users\Pedro
........\index.php on line 14
UPDATE:
When i use include the PHP returns the same error:
Fatal error: Class 'AvaliacaoLocal' not found in C:\Program
Files\VertrigoServ\www\system\index.php on line 10
i've change folder to verify if could be it.
The class is declared belonging to a namespace, you have to call it in this way:
$avaliacaoLocal = new \model\AvaliacaoLocal();
But now, the namespace is also included in $class, so the autoload function needs to handle that:
function __autoload($class)
{
$file = str_replace(array('_', '\\'), '/', $class) . '.php';
if (is_file($file)) {
require $file;
}
}
This function takes $class value and replace every \ (and _) from the namespace with a / to get the file name.

why is php class not being loaded

Im testing this thing where i'm trying to load a class and use it like this:
$this->model->model_name->model_method();
This is what I've got:
<?php
error_reporting(E_ALL);
class Loader {
public function model($model)
{
require_once("models/" . $model . ".php");
return $this->model->$model = new $model;
}
}
class A {
public $load;
public $model;
public $text;
public function __construct()
{
$this->load = new Loader();
$this->load->model('Test');
$this->text = $this->model->Test->test_model();
}
public function get_text()
{
return $this->text;
}
}
$text = new A();
echo $text->get_text();
?>
Im getting a bunch of errors here:
Warning: Creating default object from empty value in
C:\xampp\htdocs\fw\A.class.php on line 9
Notice: Trying to get property of non-object in
C:\xampp\htdocs\fw\A.class.php on line 24
Fatal error: Call to a member function test_model() on a non-object in
C:\xampp\htdocs\fw\A.class.php on line 24
What am I doing wrong? Thanks for any tip!
P.S. not much in the loaded file:
<?php
class Test {
public function test_model()
{
return 'testmodel';
}
}
?>
In the A class' constructor you are not assigning the "loaded" model to anything and later you are trying to use the $model property which has nothing assigned to it.
Try this:
class A {
public $load;
public $model;
public $text;
public function __construct()
{
$this->load = new Loader();
$this->model = $this->load->model('Test');
$this->text = $this->model->test_model();
}
(...)
Problem may be that you have not defined Loader.model as object but treating it like it is.
class Loader {
public $model = new stdClass();
public function model($model)
{
require_once("models/" . $model . ".php");
return $this->model->$model = new $model();
}
}
When you have your class like this you can use
$this->model->model_name->model_method();
Try the following code(UPDATED) if you want to avoid $this->model = $this->load->model('Test') in the constructor.
You can simply load the models by calling $this->loadModel(MODEL) function
<?php
error_reporting(E_ALL);
class Loader {
private $models = null;
public function model($model)
{
require_once("models/" . $model . ".php");
if(is_null($this->models)){
$this->models = new stdClass();
}
$this->models->$model = new $model();
return $this->models;
}
}
class A{
public $load;
public $model;
public $text;
public function __construct()
{
$this->load = new Loader();
$this->loadModel('Test');
$this->loadModel('Test2');
$this->text = $this->model->Test2->test_model();
}
public function get_text()
{
return $this->text;
}
private function loadModel($class){
$this->model = $this->load->model($class);
}
}
$text = new A();
echo $text->get_text();
?>

PHP multiple-inheritance

I try to inherit multiple classes from each other, but something wrong happens somewhere. The classes are the following:
Part of the MobilInterface class:
class MobileInterface
{
private $config;
private $errorData;
private $data;
private $output;
private $job;
public $dbLink;
public function __construct($config) {
$this->config = $config;
}
public function initialize($job) {
$this->dbLink = $this->createDbInstance($this->config);
require_once 'jobs/' . strtolower($this->config->joblist[$job]) .'.php';
$this->job = new $this->config->joblist[$job]($this);
}
public function run($params) {
$job = $this->job;
$this->data = $this->job->run($_GET);
}
}
Mobil Interface is the main interface, which calls the Kupon class based on a string in the $config. My problem is that i want more Kupon like classes and wanted to make a BaseJob class to be able to write each Job class without the constructor.
The problem is that the Kupon class can't see the $dbLink and the $config variables.
The BaseJob class:
<?php
class BaseJob
{
public $interface;
public $dbLink;
public $config;
public function __construct(MobileInterface $interface) {
$this->interface = $interface;
$this->config = $this->interface->get('config');
$this->dbLink = $this->interface->get('dbLink');
}
}
?>
And the Kupon class:
function __construct(){
parent::__construct(MobileInterface $interface);
}
}
?>

is there a way puting variables in controller from an extended framework class?

hello im still on learning mvc by makeing one, and today i realize that i have a miss on how things work.
class Framework
{
function __construct()
{
require 'libraries/language/l.php';
/*
$l['hello'] = 'hello';
$l['helloworld'] = 'helloworld';
etc
*/
}
}
class Controller extends Framework
{
function index()
{
#missing ?
echo $l;
}
}
ok the first question is how can i echo $l from my controller files ? is there a way to do that ?
edit* same for this.
function library( $lib ){
if (file_exists('libraries/lib.'. $lib .'.php')) {
require 'libraries/lib.'. $lib .'.php';
if (class_exists($lib)) {
$class = ucfirst($lib);
$$lib = new $class;
return TRUE;
}
if (!class_exists($lib)) {
return FALSE;
}
}
}
thanks for looking in.
Adam ramadhan
Pass the data through object protected properties:
class Framework
{
protected $l = array();
function __construct()
{
require 'libraries/language/l.php';
$this->l['hello'] = 'hello';
$this->l['helloworld'] = 'helloworld';
}
}
class Controller extends Framework
{
function index()
{
echo $this->l['hello'];
}
}
Well, that means for each Controller instance, you are going to keep a big array inside it.
Actually, you can make a singleton class that provides translation for text:
class Language
{
private static $instance;
public $l = array();
private function __construct() {
require 'libraries/language/l.php';
$this->l = $l;
}
public static function getInstance() {
if (!isset(self::$instance)) {
$c = __CLASS__;
self::$instance = new $c;
}
return self::$instance;
}
}
And you can have a shorthand function for it:
function l($text) {
return Language::getInstance()->l[$text];
}
And then use it:
echo l('hello') . "\n";

Categories