Solved

util is repeated twice

Posted on 2014-04-08
16
211 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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 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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

729 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