I need to send and get Class into another class like a variable in MVC method :
class Demo {
private $vars = array();
function set($var , $data) {
$this->vars[$var] = $data;
}
function request() {
extract($this->vars);
var_dump($vars);
}
}
And i want to use above class into this :
class Test extends foo {
function __construct(){
$this->demo = new Demo();
}
function register(){
$user = Load::model('user');//Now User Is An Object
$this->demo->set('user',"$user");//This Is Error
$this->demo->request();
}
Catchable fatal error: Object of class user could not be converted to string in
remove the quotes
$this->demo->set('user', $user);
And don't forget to create your variable in your class scope
class Test extends foo {
public $demo;
should do the trick
class Test extends foo {
protected $demo; // you missed to declare this variable
function __construct(){
$this->demo = new Demo();
}
function register() {
$user = Load::model('user');//Now User Is An Object
$this->demo->set('user', $user);
$this->demo->request();
}
}
Related
class MyAppClass {
protected $_config = array();
protected $_template = '';
public function init( ){
require_once('core_config.php'); // Inside is $_SC_config with an array of values
$this->_config = $_SC_config;
$this->_template = new template;
echo $this->_template->echo_base();
}
}
class template extends MyAppClass{
public function echo_base() {
var_dump($this->_config); // returns empty array
}
}
$myApp = new MyAppClass;
$myApp->init();
What's wrong with code above so
var_dump($this->_config)
in template class returns empty array after init function?
Thanks in advance.
I think you don't get object programming yet. In MyAppClass::init method you create new object of template class which extends your MyAppClass class. I have no idea what do you want to acheve but I will show you snippet which works.
<?php
class MyAppClass {
protected $_config = array();
protected $_template = '';
protected function init( ){
//require_once('core_config.php'); // Inside is $_SC_config with an array of values
$this->_config = 'foo';
}
}
class template extends MyAppClass{
public function __construct(){
$this->init();
}
public function echo_base() {
var_dump($this->_config); // returns empty array
}
}
$myApp = new template;
$myApp->echo_base();
I have this class:
class Search
{
protected static $Basics;
public function __construct() {
self::$Basics = new Basics();
}
public static function getT() {
return self::$Basics->get('keywords/t');
}
public static function isAvailable($keyword) {
return self::$Basics->get('keywords/available', ['keyword' => $keyword])['available'];
}
}
The class Basics is really simple class:
class Basics
{
public function __construct()
{
//some code..
}
public function get($keyword, $param = null)
{
return ['available' => true];
}
}
Call to getT function:
use App\Libraries\Search;
class GV
{
public function test() {
echo Search::getT() ? 'ok' : 'bad';
}
}
But, when i run the function getT in class Search, it return this error: Call to a member function get() on a non-object
What can i do?
You are calling the method inside Search statically (Search::getT();) which will never fire the __construct() method.
__construct() gets fired upon instantiating the class ($search = new Search;), not upon calling static methods (Class::method();).
Simply instantiate your search object: $search = new Search;
Like so:
use App\Libraries\Search;
class GV
{
public function test() {
$search = new Search;
echo $search::getT() ? 'ok' : 'bad';
}
}
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");
I am trying to initiate a class on demand.
class MyClass{
private $modules = array("mod1" => false);
public function __get($name){
if(array_key_exists($name, $this->modules) && !$this->modules[$name]){
$class = ucfirst($name);
$this->$name = new $class();
$this->modules[$name] = true;
}
}
}
I then have another class, which then extends the above class. If I do the following the class doesn't get initiated, and I get an error Fatal error: Call to a member function get() on a non-object
class Home extends MyClass{
public function main(){
echo $this->mod1->get("cat");
}
}
But if I do this, the class does get initiated.
class Home extends MyClass{
public function main(){
$this->mod1;
echo $this->mod1->get("cat");
}
}
Is there any way for me to initiate the class without having to add that extra line?
Just return it after it's instantiated:
class MyClass{
private $modules = array("mod1" => false);
public function __get($name){
if(array_key_exists($name, $this->modules) && !$this->modules[$name]){
$class = ucfirst($name);
$this->$name = new $class();
$this->modules[$name] = true;
return $this->$name;
}
}
}
Now you can do what you want:
class Home extends MyClass{
public function main(){
echo $this->mod1->get("cat");
}
}
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");
}
}