Solved

util is repeated twice

Posted on 2014-04-08
16
203 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
  • 8
  • 8
16 Comments
 
LVL 142

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 142

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
 

Author Comment

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

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 142

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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 142

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 142

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 142

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 142

Accepted Solution

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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 and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now