0

Im getting an error at the class' __construct, says:

Notice: Undefined variable: DEFAULT_TOP_PAGE_ID in classes.php on line XY.

Here is the code:

// consts.php
    <?php
    $DEFAULT_TOP_PAGE_ID = "top_";
    ...

// classes.php
    <?php
    error_reporting (E_ALL);
    require_once("consts.php");

    class cSiteManager {
      public $top_page_ID;

      public function __construct() {
        $this->top_page_ID = $DEFAULT_TOP_PAGE_ID;
    ...

Can anyone tell me where the problem lies?

tcxbalage
  • 696
  • 1
  • 10
  • 30

3 Answers3

1

Variables have scope. If you're trying to use a variable inside a function it will be local to the function. To use one from outside the function you need to declare it as global.

function someFunc() {
  global $DEFAULT_TOP_PAGE_ID;
  // more code...
  $this->top_page_ID = $DEFAULT_TOP_PAGE_ID;
  // etc.

}

In this case I think you probably need a definition:

define("DEFAULT_TOP_PAGE_ID", "top_");

then

function someFunc() {
  // more code...
  $this->top_page_ID = DEFAULT_TOP_PAGE_ID;
  // Note: $ has gone  ^ here
  // etc.

}
  • Thank you, its working on both way, although I'm still confused a bit about these scopes (I used to programming in Delphi, so I have a few bad habit). When I defined originally [$DEFAULT_TOP_PAGE_ID = "top_"] in the consts.php I thought it will be global and visible from every unit if I include the consts.php. And I was able to access from any code, except from the class. So I don't really understand was it global originally or not? When I defined on your way [define("DEFAULT_TOP_PAGE_ID", "top_")] in the same consts.php I was able to use it in the constructor straight away. – tcxbalage Dec 15 '13 at 05:19
0

Research scopes. $DEFAULT_TOP_PAGE_ID would have to be defined in the class or the method (function). If defined in the class, you would need to access it via $this-> like you have with top_page_ID.

m59
  • 43,214
  • 14
  • 119
  • 136
Devon Bessemer
  • 34,461
  • 9
  • 69
  • 95
0

The variable $DEFAULT_TOP_PAGE_AD appears to be defined as a global. Conseqeuently, you have to declare it global in the constructor:

public function __construct() {
    global $DEFAULT_TOP_PAGE_AD;
    $this->top_page_ID = $DEFAULT_TOP_PAGE_AD;
}
bishop
  • 37,830
  • 11
  • 104
  • 139
  • Thanks! Is this mean I have to declare all existing variable in the class' constructor as global if I want to use them inside the class? I'm revising an old code and there is dozens of this kind of variables in the consts.php. – tcxbalage Dec 15 '13 at 05:24
  • Yes. You can think of "global scope" as meaning "everywhere except inside functions and classes". This is not a strict definition, but the idea is that global scope is what you have when not in a function or class. The thing about scopes is you have to point to the variable when crossing scopes: so if you're in function scope you need to say "hey such and such variable is global". So all those variables you have in const.php need to be declared global to access them. Or switch to defines which are available everywhere (which makes them dangerous) or class or instance variables. – bishop Dec 15 '13 at 17:09
  • Lovely, thank you. Due the limited time just simply changed them to defines, might revise them later. – tcxbalage Dec 16 '13 at 22:31