Ok, I have the parent class from a plugin I'm trying to extend:
class OsBookingHelper {
public static function get_statuses_list(){
return array( LATEPOINT_BOOKING_STATUS_APPROVED => __('Approved', 'latepoint'),
LATEPOINT_BOOKING_STATUS_PENDING => __('Pending Approval', 'latepoint'),
LATEPOINT_BOOKING_STATUS_PAYMENT_PENDING => __('Payment Pending', 'latepoint'),
LATEPOINT_BOOKING_STATUS_CANCELLED => __('Cancelled', 'latepoint'));
}
}
My code to extend follows: It's not working for some reason....
if ( ! class_exists( 'OsNuuCustomAppointmentStatusHelper' ) ) :
class OsNuuCustomAppointmentStatusHelper extends OsBookingHelper {
function __construct(){
parent::__construct();
}
public static function get_statuses_list(){
return array( LATEPOINT_BOOKING_STATUS_APPROVED => __('Approved', 'latepoint'),
LATEPOINT_BOOKING_STATUS_PENDING => __('Pending Approval', 'latepoint'),
LATEPOINT_BOOKING_STATUS_PAYMENT_PENDING => __('Payment Pending', 'latepoint'),
LATEPOINT_BOOKING_STATUS_CANCELLED => __('Cancelled', 'latepoint'),
LATEPOINT_BOOKING_STATUS_COMPLETE => __('Complete', 'latepoint'));
}
endif;
Any ideas? I'm totally at a loss with this...
Oh, before I forget. There's a parent class that is instantiated in another file that calls the file containing my code using include_once
Related
In CodeIgniter framework i have two controller files:
controllerA.php and controllerB.php
I need controllerB.php to add code in controllerA.php function
I have no idea how to do it, i checked Codeigniter manual, google and stackoverflow but was not able to find solution
controllerA.php has function:
function get_permission_conditions()
{
return do_action('staff_permissions_conditions', [
'contracts' => [
'view' => true,
'view_own' => true,
'edit' => true,
'create' => true,
'delete' => true,
]);
}
I want controllerB.php to communicate with controllerA.php and add custom code example:
function get_permission_conditions()
{
//Code from controllerA.php
return do_action('staff_permissions_conditions', [
'contracts' => [
'view' => true,
'view_own' => true,
'edit' => true,
'create' => true,
'delete' => true,
//custom code from controllerB.php goes here
]);
}
You should extend controllerA like this:
class controllerA extends CI_Controller{
// All the function will go here
return do_action('staff_permissions_conditions', [
'contracts' => [
'view' => true,
'view_own' => true,
'edit' => true,
'create' => true,
'delete' => true,
]);
}
class controllerB extends controllerA{
public $permission_array;
function __construct() {
$this->permission_array = $this->do_action(); // Here $permission_array will have the array returned by controllerA's function 'do_action'
}
//custom code from controllerB.php goes here
// You can use $permission all over
}
Create a parent controller for both of the and let them both extend that parent and move that method to that parent and let the parent extend CI_Controller .. pretty much like what you did with MY_Controller ...
That's your C_Controller (Parent controller):
class C_Controller extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
// your methods that will be extended
...
}
That's your A_Controller:
class A_Controller extends C_Controller
{
public function __construct()
{
parent::__construct();
}
}
That's your B_Controller:
class B_Controller extends C_Controller
{
public function __construct()
{
parent::__construct();
}
}
i'm using codeigniter for a website project. when i include a model, it will work as long as no function is implemented (except the constructor).
this configuration works:
class Xyz_model extends CI_Model {
function __construct() {
}
}
this doesn't:
class Xyz_model extends CI_Model {
function __construct() {
}
public function get_xyz() {
return [
"xy" => ["xy"],
"yz" => ["xy"],
"zz" => ["xy","zx","zy"]
];
}
}
there is not even an database access... and i have no clue why it is not working.
You are extending the core model class, but the child's constructor is being used in placed of the parents:
parent::__construct();
Add that to your models constructor.
use this
In model
class Xyz_model extends CI_Model {
function __construct() {
}
public function get_xyz() {
$array = array(
'xy' => 'xy',
'yz' => 'xy',
'zz' => array("xy","zx","zy")
);
return $array;
}
}
In controller
$new = $this->Xyz_model->get_xyz()
print_r($new);
so output will be
Array ( [xy] => xy [yz] => xy [zz] => Array ( [0] => xy [1] => zx [2] => zy ) )
As my title, I tried call that method but I got an error:
Fatal error: Call to a member function post() on a non-object in C:\xampp\htdocs\cifirst\application\modules\front\controllers\shopping.php on line 11
If I create a controller not in module, that method I can use very easy but in this case can not (everything code in method below can not run). This is my code:
public function add_to_cart() {
$data = array(
'id' => $this->input->post('productId'), // line 11
'name' => $this->input->post('productName'),
'price' => $this->input->post('productPrice'),
'qty' => 1,
'options' => array('img' => $this->input->post('productImg'))
);
$this->load->library('MY_Cart');
$this->cart->insert($data);
//redirect($_SERVER['HTTP_REFERER']);
//echo $_POST['productId'].'-'.$_POST['productName'];
}
And this code doesn't work too:
public function __construct() {
$this->load->library('cart');
$this->load->helper('form');
}
I'm using XAMPP 1.8.1, CodeIgniter 2.1.3 and newest MX. Please help me!
When you're using CodeIgniter functions outside of controllers, models, and views you need to get an instance of Codeigniter first.
class MyClass {
private $CI;
function __construct() {
$this->CI =& get_instance();
$this->CI->load->library('cart');
$this->CI->load->helper('form');
}
public function someFunction() {
$var = $this->CI->input->post("someField");
}
}
If you are calling:
$this->input->post('productId');
inside controller than the problem is with your constructor declaration or your class name
Your construct part should contain code like this:
Class Home extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->CI->load->library('cart');
$this->CI->load->helper('form');
}
public function add_to_cart()
{
$data = array(
'id' => $this->input->post('productId'), // line 11
'name' => $this->input->post('productName'),
'price' => $this->input->post('productPrice'),
'qty' => 1,
'options' => array('img' => $this->input->post('productImg'))
);
}
}
It will work fine if you are calling from helpers function or any other classes than you should do something like this:
function __construct()
{
parent::__construct();
$this->CI->load->library('cart');
$this->CI->load->helper('form');
$this->CI =& get_instance();
}
public function add_to_cart()
{
$data = array(
'id' => $this->CI->input->post('productId'), // line 11
'name' => $this->CI->input->post('productName'),
'price' => $this->CI->input->post('productPrice'),
'qty' => 1,
'options' => array('img' => $this->CI->input->post('productImg'))
);
}
Hey guys I'm having an issue that I think is because due to namespaces, I have my parent class TheParent where I do some stuff, add it to $this then extend to a child class expecting that $this will carry over, but everything inside except what is explicitly mentioned in the parents constructor seems to vanish (the $timeout = 10) I'm trying to figure out where I borked this code at, and if anyone could explain it to me why this is not working like I would think it should?
Namespace Services;
Class TheParent
{
public function __construct($timeout = 10, array $options = array())
{
$this->setAuth($options)->setTimeout($timeout);
}
// other methods that put information into $this
public function useRest()
{
require_once 'RestAggregator.php'
$this->message = REST::getInstance();
header('Content-Type: text/plain');
print_r($this); die;
}
}
Namespace Services;
Class REST Extends TheParent
{
private static $instance = NULL;
private $messages = array();
public function __construct()
{
$this->messages = self::getDataMessages();
}
public static function getInstance()
{
if(! isset(REST::$instance))
{
REST::$instance = New REST();
}
return REST::$instance;
}
protected function getDataMessages()
{
return REST::$instance->messages = array(
'foo' => '4',
'bar' => '5',
'baz' => '6',
);
}
}
This is the rest object returned, you would THINK that I would also have data from TheParent which is where things like _appKey etcetera have already been defined before being passed to REST
Services\REST Object
(
[type] =>
[messages:Services\REST:private] => Array
(
)
[groups:Services\REST:private] => Array
(
)
[_following:protected] =>
[_sent:protected] =>
[_private:protected] =>
[_received:protected] =>
[_appKey:protected] =>
[_appSecret:protected] =>
[_authToken:protected] =>
[_authSecret:protected] =>
[_authCode:protected] =>
[_redirectUri:protected] =>
[_smAuth:protected] =>
[_accessToken:protected] =>
[_tigerToken:protected] =>
[_data:protected] =>
[_timeout:protected] => 10
[_cookieJar:protected] =>
[dbh] =>
[opts] => Array
(
)
)
You are saying that class REST extends (is a child of) class Parent. But in class Parent you are referring to methods in the child class. Child classes can use parent methods but parent classes have no access to their child classes. Extending a class is a one way street.
i have multiple php classes
// a Base class
abstract class Base_Page {
protected static $config = array(
'status' => 'int',
);
}
// an inheriting class
class Page extends Base_Page{
protected static $config = array(
'title' => 'varchar',
'description' => 'text',
);
// and one more level of inheritance
class Page_Redirect extends Base_Page {
protected static $config = array(
'href' => 'http://domain.com',
);
}
now id'd like to do this:
$page_redirect = new Page_Redirect();
$page_redirect->getConfig(); // which i assume to be implemented (this is my problem)
// should return:
// array(
// 'status' => 'int',
// 'title' => 'varchar',
// 'description' => 'text',
// 'href' => 'http://domain.com',
// )
Due to the fact that the variable gets overwrote by the extending class a dont't get how to accomplish this. Thanks for your look at it.
You cannot do this with a bare property. It would be much better to use methods instead:
abstract class Base_Page {
protected function getConfig() {
return array('status' => 'int');
}
}
// an inheriting class
class Page extends Base_Page{
protected function getConfig() {
return array(
'title' => 'varchar',
'description' => 'text',
) + parent::getConfig();
}
}
// and one more level of inheritance
class Page_Redirect extends Base_Page {
protected function getConfig() {
return array(
'href' => 'http://domain.com',
) + parent::getConfig();
}
}
Of course now you have lost the ability to get the configuration statically, but it's highly likely that this does not matter. If it does (i.e. you need to know the configuration without having an instance at hand, and it is meaningless to create one on a whim) then the code needs further refactoring.
<?php
// a Base class
abstract class Base_Page {
protected static $config = array(
'status' => 'int',
);
}
// an inheriting class
class Page extends Base_Page {
protected static $config = array_merge(
parent::$config,
array(
'title' => 'varchar',
'description' => 'text',
)
);
}
Try something like this.