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 ) )
Related
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
If I have this example :
class A {
public function test(){
echo 'a';
}
}
class B {
public function test(){
echo 'b;
}
}
class C {
public function test(){
(new A())->test();
}
}
(new A())->test();
(new B())->test();
(new C())->test();
I want to get array with all called functions and classes, something like:
[
'A' => [
'function' => 'test',
'count' => 2,
'miliseconds' => 20,
'miliseconds_each' => [5, 15],
],
'B' => [
'function' => 'test',
'count' => 1,
'miliseconds' => 25,
'miliseconds_each' => [25],
],
'C' => [
'function' => 'test',
'count' => 1,
'miliseconds' => 30,
'miliseconds_each' => [30],
]
]
Notice: I want solution for whole framework not just bad workaround for this small example.
I would do like this, Make a base class and extends to update your analysis.
<?php
class Base{
static $data;
public static function analysis(){
return this::data;
}
function update($function){
if(isset(Base::$data[$function])){
Base::$data[$function] = ['count'=>Base::$data[$function]['count']+1,'function'=>'test'];
}else{
Base::$data[$function] = ['count'=>1,'function'=>'test'];
}
}
}
class A extends Base{
public function test(){
$this->update('a');
echo 'a';
}
}
class B extends Base{
public function test(){
$this->update('b');
echo 'b';
}
}
class C extends Base{
public function test(){
(new A())->test();
}
}
(new A())->test();
(new B())->test();
(new C())->test();
print_r(Base::$data);
?>
I know you can update code to get miliseconds and miliseconds_each
Live demo : https://eval.in/879998
class Settings {
public $constants = [
'database' => [
'APP_DB_HOST' => 'localhost'
],
];
}
class Constants extends Settings {
public $database = [
'APP_DB_HOST' => $settings->constants['database']['APP_DB_HOST'], // not working
];
}
I need to access parent class array values in child class. but this $settings->constants['database']['APP_DB_HOST'] is not working for some reason.
Here is the working solution
<?php
class Settings {
public $constants = [
'database' => [
'APP_DB_HOST' => 'localhost'
],
];
}
class Constants extends Settings {
public $database;
public function __construct(){
$database = [
'APP_DB_HOST' => $this->constants['database']['APP_DB_HOST'], // working
];
}
}
print_r(new Constants());
outputs:
Constants Object
(
[database] =>
[constants] => Array
(
[database] => Array
(
[APP_DB_HOST] => localhost
)
)
)
as per your comment,
if you want to do it in other class function, you can do that as well.
class Constants extends Settings {
public $database;
public function useParentHost(){
$this->database = [
'APP_DB_HOST' => $this->constants['database']['APP_DB_HOST'], // working
];
return $this->database;
}
}
and then
$test = new Constants();
print_r($test->useParentHost());
you have to declare some function to use $this, without/outside the function this will cause an error.
The variable $settings does not exist, perhaps you mean $this?
$this->constants['database']['APP_DB_HOST'];
Enjoy.
I am trying to create a abstract class that has a property/array of settings and then in the child classes add additional properties to that property/array. I want methods defined in the abstract class to use the child classes property/array when the method is called from the child. I thought the code bellow should work... But it seems to still be accessing the property from the parent class.
abstract class AbstractClass {
protected static $myProp;
public function __construct() {
self::$myProp = array(
'a' => 10,
'b' => 20
);
}
protected function my_print() {
print_r( self::$myProp );
}
}
class ClassA extends AbstractClass {
protected static $myProp;
public function __construct() {
parent::__construct();
self::$myProp = array_merge( parent::$myProp,
array(
'c' => 30,
'd' => 40
)
);
$this->my_print( self::$myProp );
}
}
$myObj = new ClassA;
This should return Array ( [a] => 10 [b] => 20 [c] => 30 [d] => 40 )
Instead it returns Array ( [a] => 10 [b] => 20 )
How do I get this to work?!?
Like so:
<?php
abstract class AbstractClass {
protected static $myProp;
public function __construct() {
self::$myProp = array(
'a' => 10,
'b' => 20
);
}
protected function my_print() {
print_r( self::$myProp );
}
}
class ClassA extends AbstractClass {
public function __construct() {
parent::__construct();
self::$myProp = array_merge( parent::$myProp,
array(
'c' => 30,
'd' => 40
)
);
$this->my_print( self::$myProp );
}
}
$myObj = new ClassA;
You are overwriting the variable
Actually in parent class you did not define method my_print() to contain any arguments, plus, the method uses self::$myProp (not static::).
Also, as Ka_lin already answered you do not need to redeclare property that has been declared in parent class.
If you do (for some reason need to redeclare it like set predefined value different than parent) you can do two things:
For one, you can change my_print() to accept argument then print_r() the argument not self::$myProp:
protected function my_print($debug) {
print_r($debug);
}
Or, (in PHP as of version 5.3.0.) you can use static:: instead of self:::
protected function my_print() {
print_r(static::$myProp);
}
I would go for the second option.
Also you should ready more about self vs static (late binding) in php manual
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.