Solved

util is repeated twice

Posted on 2014-04-08
16
209 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Wordpress Pagination 1 45
Could you point a way to eliminate an array unexpected element? 8 41
Change a button after e-mail 4 22
MySQL-Design Help 12 44
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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 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…

726 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