Wordpress add_action from another class - php

This is my code:
class Show
{
public function error()
{
add_action('admin_notices', [$this, 'pip1']);
}
public function pip1() {
echo '
<div class="notice notice-success"><p>test</p></div>
';
}
}
(new Show())->error();
Top code works fine and print errors.
But if I use this code in another class, it doesn't work.
Example:
class Manager extends Controller
{
public function add()
{
(new Show())->error();
}
}
(new Manager())->add();

You should include class file first before call it.
class Manager extends Controller
{
public function add()
{
require_once('path/to/class_Show/Show.php');
(new Show())->error();
}
}
(new Manager())->add();

Related

why fatal error like using $this when not in object content?

I have a problem with the following code. When I use $this when not in object content.
dashboardController.php
<?php
class dashboardController extends BaseController{
public function index($name=''){
$this->view->loadView('dashboard/index');
}
}
baseController.php
<?php
class BaseController{
public function loadView($viewName){
$this->view = new View($viewName);
}
}
view.php
<?php
class View{
public function __construct($viewName);{ echo " i am form view to render
}
}
I am getting the error using $this when not in object content but in another folder the login went success without.
Try this:
dashboardController
<?php
class dashboardController extends BaseController
{
public function index($name = '')
{
// Call loadView(), because this controller doesn't have a method
// called loadView() it'll fall back to loadView() in the BaseController
$this->loadView('dashboard/index');
// Get the viewName from the view, will be 'dashboard/index'
echo $this->view->getViewName();
}
}
BaseController
<?php
class BaseController
{
// The view, protected so it can be accessed by children via $this->view
protected $view;
// Load a view, protected so it can only be accessed by children
// or within this controller
protected function loadView($viewName)
{
// Create a new view instance
$this->view = new View($viewName);
}
}
View
<?php
class View
{
// The loaded view, private so it can't be changed by an external class
private $viewName;
public function __construct($viewName)
{
$this->viewName = $viewName;
}
// Retrieve the view name, public so anything can access it
public function getViewName()
{
return $this->viewName;
}
}

prestashop - Loading a smarty template in a tab

What I'm trying to attempt is to display a template on a custom tab.
This is the code of controllers/admin/AdminTController.php:
<?php
class AdminTController extends ModuleAdminController {
public function __construct() {
parent::__construct();
}
public function display() {
//echo $this->l('This is admin my module tab !');
// the echo works.
$this->createTemplate('initial.tpl');
}
}
This is code of /views/templates/admin/initial.tpl:
<p>This is just a test</p>
The result of this code is a blank page.This means the template is not loaded. What am I doing wrong?
Try using the below mentioned code.
<?php
class AdminTController extends ModuleAdminController {
public function __construct() {
parent::__construct();
}
public function display() {
$tpl = $this->custom_smarty->createTemplate('initial.tpl');
return $tpl->fetch();
}
}
?>

Unable to locate the specified class: Session.php, When call a function from another controller in Codeigniter

contrller:News.php
This is my controller News
<?php class News extends CI_Controller {
public function __construct()
{
}
public function getShowIN_News()
{
return $result;
} } ?>
contrller:Category.php
This is my controller Category
<?php class Category extends CI_Controller {
public function __construct()
{
}
public function category()
{
require('news.php');
$test = new News();
$data["headlines"] = $test->getShowIN_News();
} }?>
By using an empty constructor, you're making it so that CI_Controller::__construct() isn't called, and that's where everything in the framework is initialized.
I know you've put it there to hack it so you can call one controller from another, but it is very intentionally made that way, exactly so you don't do this.

Codeigniter calling model method from another model causes error

I have a controller:
class Blah extends Controller
{
function Blah()
{
$this->load->model('baga_model');
}
}
then comes baga_model:
class Baga_model extends Model
{
function do_it()
{
echo "BOOM!";
}
}
..and
class Blah_model extends Model
{
function some_action()
{
$this->baga_model->do_it();
}
}
So .. when in blah_model I call $this->baga_model->do_it() I get an error :
Call to a member function do_it() on a non-object
I just can't understand why.... I know it must work, I did something similar before..
Thanks
Got it! I had to load baga_model in blah_model constructor. This way it works.
Thanks everyone.
public function test()
{
$this->load->model('baga_model');
$this->baga_model->do_it();
}
Model
class baga_model extends CI_Model
{
public function do_it()
{
echo $this->bar("BOOM!");
}
Your not loading your required model inside your model:
class Blah_model extends CI_Model
{
$this->baga_model = $this->load->model('baga_model', true);
public function some_action()
{
$this->baga_model->do_it();
}
}

open cart how to get current customer info?

I have created following controller:
class ControllerCommonTestSMS extends Controller
{
function index()
{
// action
}
}
But if I add the following line, it throws an error that the function is undefined:
$this->customer->isLogged();
class ControllerCommonTestSMS extends Controller
{
function index()
{
if (!$this->customer->isLogged()) {
//code to be execu
}
}
}
this is working fine.

Categories