6

Say we have the following:

some.class.php

class
{
    public __construct()
    {
        fun_stuff();
    }

}

configuration.inc

const SOMECONST = 1;
const SOMEOTHERCONST = 2;

I am looking to do something like this:

some.class.php

class
{
    public __construct()
    {
        include_once(configuration.inc);
        fun_stuff();
    }

}

Now this works, but the constant is not defined within the scope of the class (echo some::SOMECONST;) but rather in the global scope (echo SOMECONST;)

I really really want to have the constants in another file as it makes a lot of sense in my case. Is there a way to declare the constants in the scope of the class? I know it's impossible to use includes or requires inside the class definition so i'm at a loss.

maxhb
  • 8,554
  • 9
  • 29
  • 53
Matthew Goulart
  • 2,873
  • 4
  • 28
  • 63
  • One issue you will run into is that you are trying to do this in the constructor - http://stackoverflow.com/questions/7145198/how-to-define-constant-in-class-constructor. – Scott Jan 29 '16 at 14:40
  • It is also impossible to include a file in the class definition (ie putting the include inside the class, but ouside a function)- http://stackoverflow.com/questions/1957732/can-i-include-code-into-a-php-class. – Scott Jan 29 '16 at 14:41
  • That's why i said "I know it's impossible to use includes or requires inside the class definition" – Matthew Goulart Jan 29 '16 at 14:41
  • The only way I see to do this, is by not using const but using normal static variables instead. Your include file could changed to `self::SOMEVAR = 1` and it should work. – Scott Jan 29 '16 at 14:43
  • @Scott I thought it would but it doesn't seem to. It throws a syntax error complaining about the "=" – Matthew Goulart Jan 29 '16 at 14:46
  • If you hadn't a cluttered name space, you could define constants in a namespaced file that you include. A little better than global, but not to the scope of the class. – Progrock Jan 29 '16 at 18:53

3 Answers3

6

Simplest possibilty is to define your constant in one class and let your other class extend that class.

class myClassConstant {
  const SOMECONST = 1;
  const SOMEOTHERCONST = 2;
}

class myClass extends myClassConstant {

  public function __construct() {
    echo self::SOMECONST . ' + ' . self::SOMEOTHERCONST . ' = 3';
  }
}

$obj = new myClass(); // Output: 1 + 2 = 3

If you are using php autoloader this can easily be split up into two different files.

maxhb
  • 8,554
  • 9
  • 29
  • 53
  • Although not an answer to my question, this seems like the most logical and viable way to accomplish what I am looking for. Thank you! – Matthew Goulart Jan 29 '16 at 14:57
  • IMHO the best and most robust solution possible. It's a pitty that traits don't allow constants as that would be a really cool and elegant solution to your problem. – maxhb Jan 29 '16 at 14:58
  • See: http://stackoverflow.com/questions/24357985/php-traits-defining-generic-constants – Progrock Jan 29 '16 at 18:52
2

How about something simple like this:

class Foo
{
    public $config;

    public __construct($config)
    {
        $this->config = $config;
        fun_stuff();
    }

    public function Bar()
    {
        echo $this->config['baz'];
    }

}

$foo = new Foo(include_once 'config.php');

config.php

<?php
return array('baz' => 'hello earth');

It's not very explicit though. There are no contracts on the config.

Progrock
  • 7,373
  • 1
  • 19
  • 25
0

Simply put, it is not possible without extensions to php. I ended up just defining the consts in the class file its self.

Matthew Goulart
  • 2,873
  • 4
  • 28
  • 63