[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 221
  • Last Modified:

util is repeated twice

<?php

namespace com\getinstance\util;

class Debug {
  public static $property=1;
    static function helloWorld() {
        print "hello from Debug\n";
    }
}

namespace main;
use com\getinstance\util;
util\Debug::helloWorld();
echo util\Debug::$property;

Open in new window


I do not understand why line 14,15
repeats
'util'
Looks like
use com\getinstance\util\util\Debug::helloWorld();
use com\getinstance\util\util\Debug::$property;
0
rgb192
Asked:
rgb192
  • 8
  • 8
1 Solution
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
if the only namespace where Debug is existing is util, you don't have to specify util...
however, if you have several namespaces in your project, and 2 (or more) of them have a Debug method, you should indeed (fully) qualify it, hence the usage of util/Debug.
you could have written also:
com\getinstance\util\Debug::xxxx
or even a fully qualified call:
\com\getinstance\util\Debug::xxxx  

hope this helps
0
 
rgb192Author Commented:
can you copy and paste your code edit so I can compare to original
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
not sure what you mean? but I guess you want to see:
<?php

namespace com\getinstance\util;

class Debug {
  public static $property=1;
    static function helloWorld() {
        print "hello from Debug\n";
    }
}

namespace main;
use com\getinstance\util;
com\getinstance\util\Debug::helloWorld();
echo com\getinstance\util\Debug::$property;

Open in new window

respectively:
<?php

namespace com\getinstance\util;

class Debug {
  public static $property=1;
    static function helloWorld() {
        print "hello from Debug\n";
    }
}

namespace main;
use com\getinstance\util;
\com\getinstance\util\Debug::helloWorld();
echo \com\getinstance\util\Debug::$property;

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
rgb192Author Commented:
Is com local and \com global
And line 13 'use' can contain more or fewer folders
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
local and global are not the right terms, it's relative and absolute.
use will be for 1 folder, you might use several "use" statements, 1 per line.
0
 
rgb192Author Commented:
local and global are not the right terms, it's relative and absolute.
in the zandstra book
local was same file and global was the require_once file
I forget which had
\

use will be for 1 folder, you might use several "use" statements, 1 per line.
use com\getinstance\util;

and
use com
use getinstance
use util

are equal??
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
no, the 2 approaches are NOT the same
0
 
rgb192Author Commented:
in the zandstra book
local was same file and global was the require_once file
I forget which had
\

you call it relative and absolute
I do not understand when \ is relative and absolute versus when \ is local and global
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
having \ in the beginning is "always" (at least as far as I know) absolute, at least in the meaning of path. again I don't understand why you refer to "local" and "global" in that context? "local" refers to the local "scope" (which can the the current file, for example, or even only the current procedure/function/namespace), while global refers to the whole script, including the included files, respectively what has been defined outside your function/namespace.
0
 
rgb192Author Commented:
from matt zandstra php book
<?php
namespace {
    class Lister {
        public static function helloWorld() {
            print "hello from global\n";
        }
    }
    class Lister2{
        public static function helloWorld(){
            print "hello from global lister2";
        }        
    }
}
namespace com\getinstance\util {
    class Lister {
        public static function helloWorld() {
            print "hello from ".__NAMESPACE__."\n";
        }
    }

    Lister::helloWorld();  // access local
    \Lister::helloWorld(); // access global
    \Lister2::helloWorld();
}
?>

Open in new window



<?php
namespace com\getinstance\util;
require_once 'global.php';
class Lister {
    public static function helloWorld() {
        print "hello from ".__NAMESPACE__."\n";
    }
}

Lister::helloWorld();  // access local
\Lister::helloWorld(); // access global
?>

Open in new window


global.php
<?php
// no namespace

class Lister {
    public static function helloWorld() {
        print "hello from global\n";
    }
}
?>

Open in new window



this book is my only point of reference
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
"global" and "local" are ony referring to the script file, "local" meaning "the code is in this local/same file", while "global" refers to the "total scope, including all other included code files"
0
 
rgb192Author Commented:
okay now I understand that
local is in same file/class/method
and
global is anywhere included


but what makes
/ into global
and '' into local


for example where is it written that
/global
local

where the only difference is '/'
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
again: in the script sample the words "global" and "local" refer to what I said, though it's not the "usual" meaning.

NORMALLY, global refers to when a variable is in the "global scope", so reachable everywhere in the code; while local refers to a variable only reachable and available in the "local scope", which is either the class, procedure or even only a code block.

and AGAIN:
the / and "" do NOT mean "global" or "local", but ABSOLUTE and RELATIVE path for the class resolution. those terms are not synonyms or interchangeable.

the leading "/" indicates that a absolute path is referred to, so the path has to be exactly as indicated. /<something>/<sub-something> means the namespace root is <something>
<something>/<sub-something>  may mean that the "<something>" may be inside some other namespace...
0
 
rgb192Author Commented:
require_once 'global.php';
\Lister::helloWorld(); // access global

would the absolute path be
/global.php


but the local
should be
Lister::helloWorld();  // access local
global.php

it is
require_once 'global.php';
not
require_once '/global.php';
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
there, the "absolute path" or "relative path" is referring to the positions of the .php files in regards to the file structure, and not in regards to the "scope" in regards to variables, classes or functions.

/global.php would refer to a file global.php in the root folder (though eventually the web's root folder, not necessarily the real / root folder of the file system)
0
 
rgb192Author Commented:
okay global / is root
local is the nearest namespace

and then there is absolute and relative


thanks
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 8
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now