Using $this when not in object context. wordpress plugin - php

I am building a wordpress plugin in some MVC style and i have setup almost every thing but when using call_user_func to call the request action class, I am not able to use the $this with the requested class.
Here is my code so far...
class Action{
protected $class;
protected $method;
protected $args = array();
protected $template='settings/index';
public function __construct() {
$this->getRequest();
}
public function getRequest(){
if(isset($_GET['c'])){
$route = $_GET['c'];
$parts = explode('/',$route);
if(isset($parts[0]))
$this->class = $parts[0];
if(isset($parts[1]))
$this->method = $parts[1];
if(isset($parts[2]))
$this->args[] = $parts[2];
if(isset($parts[3]))
$this->args[] = $parts[3];
}
}
public function render(){
extract($this->data);
ob_start();
require(LINKWAG_VIEW .DS. $this->template . P);
$this->output = ob_get_contents();
ob_end_clean();
return $this;
}
public function output(){
echo $this->output;
}
}
class Grv extends Action{
public function __construct() {
parent::__construct();
add_action('admin_menu', array($this,'setup'));
}
public function setup(){
add_menu_page( 'LinkWag', 'LinkWag', 'manage_options', 'linkwag',array($this,'execute'), plugins_url( 'myplugin/images/icon.png' ), 6 );
}
public function execute(){
if($this->class){
$this->controller = call_user_func_array(array($this->class,$this->method), $this->args);
}
}
}
The requested class goes here
class Settings extends Grv{
public function __construct() {
//parent::__construct();
}
public function view($id=false){
$this->template = 'settings/view'; // using $this here craetes a fatal error
$this->render();
}
}
suppose i requested
admin.php?page=grv&c=settings/view/2
please tell me what i am doing wrong..

use $this->render() instead of $this->class->render();
another is: add ucfirst because your class name is in first letter capital and you are passing small case in arguments.
$this->controller = call_user_func_array(array(ucfirst($this->class), $this->method), $this->args)

Related

Error in MVC of PHP

<?php
class Model{
public $string;
public function __construct(){
echo "Constructor is called";
$string = "MVC + PHP = Awesome!";
}
}
class View{
private $model;
private $controller;
public function __construct($controller, $model){
$this->controller = $controller;
$this->model = $model;
}
public function output(){
echo "<p> The string is " . $this->model->string . "</p>";
}
}
class Controller{
public $model;
public function __construct($model){
$this->model = $model;
}
}
$model = new Model();
$controller = new Controller($model);
$view = new View($controller, $model);
$view->output();
?>
I am trying to learn MVC framework in PHP. But I am facing this problem.
The constructor of the model is called but I dont get any output of $this->model->string in "view->output".
Thanks
$string is just a variable in a scope of a function, $this->string is a property.
class Model{
public $string;
public function __construct(){
echo "Constructor is called";
$this->string = "MVC + PHP = Awesome!"; // note change here
}
}

Getting __construct() must be an instance of App\Models\Page, none given message

When trying to access a function in another controller, I am getting a __construct() must be an instance of App\Models\Page, none given error message and I am wondering how to solve this...
The controller where the function is called from:
use App\Http\Controllers\Frontend\PagesController;
...
class ModulesController extends Controller {
public function filter(Request $request) {
$items = $this->loadFilteredItems($request->module_id, $request->filters);
$page = new PagesController();
$header = $page->loadElements($request->id, false);
return View::make(
'base.frontend.' . config('folder') . '.includes.filter',
compact('items', 'header'));
}
}
}
The controller that stores the function:
class PagesController extends Controller {
private $page;
public function __construct(Page $page) {
$this->page = $page;
}
private function loadElements($id) {
return 'something';
}
}
Adapted the Controllers into this:
class ModulesController extends Controller {
public function filter(Request $request) {
$items = $this->loadFilteredItems($request->module_id, $request->filters);
$page = new PagesController();
$header = $page->loadHeaderElements($request->id);
return View::make(
'base.frontend.' . config('folder') . '.includes.filter',
compact('items', 'header'));
}
}
}
class PagesController extends Controller {
/**
Removed these lines
private $page;
public function __construct(Page $page) {
$this->page = $page;
}
**/
public static function loadHeaderElements($id) {
return $this->loadElements($id);
}
private function loadElements($id) {
return 'something';
}
}
And it's working now

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();
?>

My mixin class is causing my classes to become static - PHP

So this is my mixin class:
class AisisCore_Loader_Mixins {
private $_classes;
private $_class_objects = array();
private $_methods = array();
public function __construct(){
$this->init();
}
public function init(){}
public function setup($class){
if(!is_array($class)){
throw new AisisCore_Loader_LoaderException('Object passed in must be of type $class_name=>$params.');
}
$this->_classes = $class;
$this->get_class_objects();
$this->get_methods();
}
public function get_class_objects(){
foreach($this->_classes as $class_name=>$params){
$object = new ReflectionClass($class_name);
$object_name = get_class($object);
$this->_class_objects[$object->name] = $object->newInstanceArgs($params);
}
}
public function get_methods(){
foreach($this->_class_objects as $class_object_name => $class_object){
$this->_methods[$class_object_name] = get_class_methods($class_object);
}
return $this->_methods;
}
public function __call($name, $param = null){
foreach($this->_methods as $class_name=>$methods){
foreach($methods as $method){
if($name === $method){
return $this->isParam($class_name, $method, $param);
}
}
}
throw new AisisCore_Loader_LoaderException("Method: " .$name.
" does not exist or it's access is not public");
}
private function isParam($class_name, $method, $param){
if($param != null){
call_user_func(array($class_name, $method), $param);
}else{
call_user_func(array($class_name, $method));
}
}
}
Pretty simple stuff, load a set of classes, allow you to call their functions and so on, but we have a new issue. It seems that classes passed into this are instantiated as static, thus their methods cannot use $this-> they are resorted to using self:: which is wrong.
Lets see an example of how this all works:
class BaseBridge extends AisisCore_Loader_Mixins{
public function __construct(){
parent::construct();
$this->setup(array('ClassB' => array()));
}
}
Lets Define ClassB
class ClassB{
public function __construct(){}
public function some_method(){
$this->_some_private_method();
}
private function _some_private_method(){}
}
Pretty basic stuff, so lets hook it all up in ClassA
class ClassA extends BaseBridge{
public function __construct(){
parent::__construct();
$this->some_method();
}
}
Quick Review: We have a core class, ClassA which extends BaseBridge which is our bridge class between one or more (meant to be used with more) classes that ClassA extends from. In this case were only extending from ClassB for simplicity.
Whats the issue? See, how in ClassB, were doing: $this->_some_private_method(); ya that's going to epically and catastrophically fail. Why? because I get the error: Using $this when not in object context which makes me so confused, so I change it to: self::$_some_private_method(); and it works like a charm.
Why? and what do I have to change or fix to make it so that $this can be used in a class being instantiated through the mixin class?
So with some slight modifications, I have managed to make this work. How ever I do not believe that a function with multiple arguments will work - Feed back appreciated.
class AisisCore_Loader_Mixins {
private $_classes;
private $_class_objects = array();
private $_methods = array();
public function __construct(){
$this->init();
}
public function init(){}
public function setup($class){
if(!is_array($class)){
throw new AisisCore_Loader_LoaderException('Object passed in must be of type $class_name=>$params.');
}
$this->_classes = $class;
$this->get_class_objects();
$this->get_methods();
}
public function get_class_objects(){
foreach($this->_classes as $class_name=>$params){
$object = new ReflectionClass($class_name);
$this->_class_objects[$object->name] = $object->newInstanceArgs($params);
}
}
public function get_methods(){
foreach($this->_class_objects as $class_object_name => $class_object){
$this->_methods[$class_object_name] = get_class_methods($class_object);
}
return $this->_methods;
}
public function __call($name, $param = null){
foreach($this->_methods as $class_name=>$methods){
foreach($methods as $method){
if($name === $method){
return $this->_is_param($class_name, $method, $param);
}
}
}
throw new AisisCore_Loader_LoaderException("Method: " .$name.
" does not exist or it's access is not public");
}
private function _is_param($class_name, $method, $param){
if($param != null){
$this->_param_is_array($class_name, $method, $param);
}else{
call_user_func(array($this->_class_objects[$class_name], $method));
}
}
private function _param_is_array($class_name, $method, $param){
if(is_array($param)){
call_user_func_array(array($this->_class_objects[$class_name], $method), $param);
}else{
call_user_func(array($this->_class_objects[$class_name], $method, $param));
}
}
}
Now functions inside of classes that are registered by this class can use $this->.
The issue is that I am not sure if multiple param based functions will actually work.

Php Class - How can I make a Class know parent value

<?php
class FirstClass{
public static $second;
public static $result = 'not this =/';
public function __construct(){
$this->result = 'ok';
$this->second = new SecondClass();
}
public function show(){
echo $this->second->value;
}
}
class SecondClass extends FirstClass{
public $value;
public function __construct(){
$this->value = parent::$result; //Make it get "ok" here
}
}
$temp = new FirstClass();
$temp->show(); //It will show: "not this =/"
?>
How can I make it to print "ok"?
I mean, the SecondClass should know what FirstClass set as result, see?
Replace $this->result = 'ok'; with self::$result = 'ok'; in FirstClass constructor.
Btw, the code is terrible. You're mixing static and instance variables, and extend classes but don't use benefits extension provides.
you need to reference the static as self::$result in the first class.
Below should do what you want...
<?php
class FirstClass{
public static $second;
public static $result = 'not this =/';
public function __construct(){
self::$result = 'ok';
$this->second = new SecondClass();
}
public function show(){
echo $this->second->value;
}
}
class SecondClass extends FirstClass{
public $value;
public function __construct(){
$this->value = parent::$result; //Make it get "ok" here
}
}
$temp = new FirstClass();
$temp->show(); //It will show: "not this =/"
?>

Categories