?
Solved

util is repeated twice

Posted on 2014-04-08
16
Medium Priority
?
215 Views
Last Modified: 2014-04-23
<?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
Comment
Question by:rgb192
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 8
16 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39987943
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
 

Author Comment

by:rgb192
ID: 39993372
can you copy and paste your code edit so I can compare to original
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39993397
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:rgb192
ID: 39996329
Is com local and \com global
And line 13 'use' can contain more or fewer folders
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39996473
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
 

Author Comment

by:rgb192
ID: 39997966
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
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39998319
no, the 2 approaches are NOT the same
0
 

Author Comment

by:rgb192
ID: 39999582
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
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 40000851
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
 

Author Comment

by:rgb192
ID: 40005745
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
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 40008548
"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
 

Author Comment

by:rgb192
ID: 40009281
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
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 40014127
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
 

Author Comment

by:rgb192
ID: 40014772
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
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 40014832
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
 

Author Closing Comment

by:rgb192
ID: 40019106
okay global / is root
local is the nearest namespace

and then there is absolute and relative


thanks
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question