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);
}
}
?>
Related
Let's say that we have a MVC pattern. If we have something like this:
blog_controller.php
<?php
class c_blog{
private $model;
public function __construct(){
include( [model_path] );
$this->model = new m_blog();
}
public function post_list(){
return $this->model->get_list();
}
}
blog_model.php
<?php
class m_blog{
public function get_list(){
global $db;
$sel = $db->query("...");
$sel = $db->fetch_array($sel);
return $sel;
}
}
index.php
<?php
$db = new database();
include "blog_controller.php";
$blog = new c_blog();
var_dump( $blog->post_list() );
?>
Why the above example isn't good, and a lot of devs want OOP like class m_blog extends database{} ?
I want only the advantages and disavantages, because I use this "worry" method on over 15 sites with a high traffic and I don't have problems (some of that websites work with multidatabases and it's very easy to change global $db, to global $db_five.)
Thanks!
Why not use inheritance?
<?php
class model {
private $db;
public function __construct() {
$this->initDb();
}
private function initDb();
}
class c_blog {
private $model;
public function __construct(){
include( [model_path] );
$this->model = new m_blog();
}
public function post_list(){
return $this->model->get_list();
}
}
class m_blog extends model{
public function __construct() {
parent::__construct;
}
public function get_list(){
$sel = $this->db->query("...");
$sel = $this->db->fetch_array($sel);
return $sel;
}
}
I am getting error in below code, cause am not able to access $log in static function Log which gets initialized in _construct.
class Logger extends Singleton{
protected function __construct() {
if(!class_exists("Log")) {
include '/usr/php/Log.php';
}
$MONITORING_LOGFILE = "/var/log/Monitoring.log";
ini_set('error_log', 'syslog');
openlog($MONITORING_LOGFILE, LOG_NDELAY, LOG_LOCAL0);
$log = Log::singleton('syslog', LOG_LOCAL0, $MONITORING_LOGFILE, array('lineFormat' => ' %{message}'), PEAR_LOG_DEBUG);
}
public static function Log($message){
$log->err($message);
}
}
Ok, I modified the above code
class Logger extends Singleton{
private $log;
protected function __construct() {
if(!class_exists("Log")) {
include '/usr/php/Log.php';
}
$MONITORING_LOGFILE = "/var/log/Monitoring.log";
ini_set('error_log', 'syslog');
openlog($MONITORING_LOGFILE, LOG_NDELAY, LOG_LOCAL0);
$this->log = Log::singleton('syslog', LOG_LOCAL0, $MONITORING_LOGFILE, array('lineFormat' => ' %{message}'), PEAR_LOG_DEBUG);
}
public function Log($message){
$this->log->err($message);
}
}
and now its working fine .... just want to confirm if initializng like this is ok in Singleton pattern?
To be able to access the $log variable trough a static function you need to have a reference of it:
class Logger extends Singleton{
private static $log; //static instance of Log::singleton
protected function __construct() {
if(!class_exists("Log")) {
include '/usr/php/Log.php';
}
$MONITORING_LOGFILE = "/var/log/Monitoring.log";
ini_set('error_log', 'syslog');
openlog($MONITORING_LOGFILE, LOG_NDELAY, LOG_LOCAL0);
self::$log = Log::singleton('syslog', LOG_LOCAL0, $MONITORING_LOGFILE, array('lineFormat' => ' %{message}'), PEAR_LOG_DEBUG);
}
//static method
public static function Log($message){
self::$log->err($message);
}
}
To create your instance of the class Logger and access the static Log function you can do the following:
$mylog = new Logger();
$mylog::Log("Your text here");
Edit: Original example removed as it was complex.
The codes provided below doesn't work. I am trying to access the methods defined in a class which is declared in the parent class.
Here is a sample code. Its not working and I'd like to know why
<?php
function & get_instance()
{
return Main::get_instance();
}
class Db{
function select($var)
{
echo $var;
}
}
class Main
{
public $db ;
public $process ;
private static $instance;
function __construct()
{
self::$instance = &$this;
$this->db = new Db ;
$this->process = Process;
}
public static function & get_instance()
{
return self::$instance;
}
}
class Process{
private $main ;
function __construct()
{
$this->main = get_instance() ;
}
function processPayment()
{
$this->main->db->select("hello");
}
}
$main = new Main ;
$main->process->processPayment();
To access members of a parent class, you will have to declare those members protected or public.
For example:
public var $db;
protected var $orders;
I have this code and i´m trying to use a object
<?php
class Controller {
public $_view;
public function __construct() {
$this->_view = new View();
return $this->_view;
}
}
class View {
public $_params = array ();
public function set_params($index_name,$valores) {
$this->_params[$index_name] = $valores;
}
public function get_param($index_name){
return $this->_params[$index_name];
}
}
?>
i would like to do this:
class Index extends Controller {
public function index() {
$model = Model::get_estancia();
$usuarios = $model->query("SELECT * FROM usuarios");
$this->_view->set_params(); // cant be used.
$this->load_view("index/index");
}
}
i would like to use the set_parms function.
but i can't see the View Function, then i can not use.
Can someone explain and advise me a good and safe way?
Correction from Phil: If a __construct() method isn't found, PHP will revert to legacy constructor syntax and check for a method with the same name as the object. In your case the method index() is being treated as the constructor, and is preventing the parent's constructor from loading the view object into the $_view property.
You can force a class to inherit a parent's constructor by defining __construct() in the child and calling the parent's constructor:
public function __construct() {
parent::_construct();
}
Here is the fixed code:
<?php
class Controller {
public $_view;
public function __construct() {
$this->_view = new View();
return $this->_view;
}
}
.
class View {
public $_params = array ();
public function set_params($index_name,$valores) {
$this->_params[$index_name] = $valores;
}
public function get_param($index_name){
return $this->_params[$index_name];
}
}
.
class Index extends Controller {
public function __construct() {
parent::__construct();
}
public function index() {
$model = Model::get_estancia();
$usuarios = $model->query("SELECT * FROM usuarios");
$this->_view->set_params(); // cant be used.
$this->load_view("index/index");
}
}
I have an abstract base Controller class and all action controllers are derived from it.
Base Controller class at construction initializes View object. This View object is used by all action controllers. Each action controller have different dependencies (this is solved by using DI container).
The problem is that base Controller class also needs some dependencies (or parameters),
for example, path to view folder. And the question is - where and how to pass parameters to base Controller class?
$dic = new Dic();
// Register core objects: request, response, config, db, ...
class View
{
// Getters and setters
// Render method
}
abstract class Controller
{
private $view;
public function __construct()
{
$this->view = new View;
// FIXME: How / from where to get view path?
// $this->view->setPath();
}
public function getView()
{
return $this->view;
}
}
class Foo_Controller extends Controller
{
private $db;
public function __construct(Db $db)
{
$this->db = $db;
}
public function barAction()
{
$this->getView()->some_var = 'test';
}
}
require_once 'controllers/Foo_Controller.php';
// Creates object with dependencies which are required in __construct()
$ctrl = $dic->create('Foo_Controller');
$ctrl->barAction();
This is just a basic example. Why is the $view private? Is there a good reason?
class View {
protected $path;
protected $data = array();
function setPath($path = 'standard path') {
$this->path = $path;
}
function __set($key, $value) {
$this->data[$key] = $value;
}
function __get($key) {
if(array_key_exists($key, $this->data)) {
return $this->data[$key];
}
}
}
abstract class Controller {
private $view;
public function __construct($path)
{
$this->view = new View;
$this->view->setPath($path);
}
public function getView()
{
return $this->view;
}
}
class Foo_Controller extends Controller {
private $db;
public function __construct(Db $db, $path)
{
// call the parent constructor.
parent::__construct($path);
$this->db = $db;
}
public function barAction()
{
$this->getView()->some_var = 'test';
}
public function getAction() {
return $this->getView()->some_var;
}
}
class DB {
}
$con = new DB;
$ctrl = new Foo_Controller($con, 'main');
$ctrl->barAction();
print $ctrl->getAction();