1
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.

2
  • stackoverflow.com/questions/1944827/…. Refer in this one. Hope it helps you. Commented Aug 14, 2015 at 5:00
  • Referring to a declared $variable as a $constant doesn't make it a "constant" -- it is still a variable. Commented Nov 6, 2023 at 21:47

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

7 Comments

thanks. is this the only way to put array inside constructor?
second approach is more cleaner, and yields more clean output, Thank to @Dan
can you please tell one more thing. outside the the class i did $constants = new Constants(); and echo $constants->constants['database']['APP_DB_HOST']; it is working but not $constants->database['APP_DB_HOST'];why is it?
if you use second approach, you can get localhost by doing this $test = new Constants(); $constants = ($test->useParentHost()); echo $constants['APP_DB_HOST'];
i tried this but it is showing empty. i am looking to find the problem.
|
0

The variable $settings does not exist, perhaps you mean $this?

$this->constants['database']['APP_DB_HOST'];

Enjoy.

3 Comments

directly using $this inside a class outside of function will cause an error
Ahh yeas, nice spot! Didn't notice he was trying to initialise a property. However wouldn't the correct syntax for your answer be $this->database = $this->constants[X]?
yes, but if we use $this->database, it yields output like this Constants Object ( [database] => Array ( [APP_DB_HOST] => localhost ) [constants] => Array ( [database] => Array ( [APP_DB_HOST] => localhost ) ) )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.