Jump to content

Recommended Posts

I have some questions about the scope of navigation :

 

if i define a constant like define('constant'), php manuel says it's global,

 

does this mean that i can use of different files also? like, i defined in define.php and used in anotherfile.php, or shoud the latter file be included then?

 

what about file

 

a.php includes file b.php which in turn includes file c.php. Can functions of a use variables of file c?

 

ultimately:

 

what about variables that are declared global inside a function but this variables is not initialized in that script, like in wordpress:

 

function date_i18n( $dateformatstring, $unixtimestamp = false, $gmt = false ) {

global $wp_locale;

 

// more code

 

}

 

$wp_locale can be initialized in another script file?

 

thanks in advance

 

 

 

 

On the back of another thread with similar questions - ignore the word global, don't even think about coding with it.

 

Right, on to your other questions, first some sample code for ilustration:

<?php
$name = getName('my Name');
include 'page2.php'; //page2.php has the function getName($name) in it
$anotherName = getName('my Name');
?>

$name will fail, $anotherName will work.  The code is efectivly parsed in a top down manner, so it will process the $name line then process every line in the page2.php then process the $anotherName line. This means that PHP has no idea about the getName() function untill after it has passed the $name line. However, PHP can "scan" the current page for the function definition and run it from anywhere on the same page as it is currently processing.  This is an over simplification, but it should give you the idea.

 

If the file is not included, and there is no other process used to access the code contained within that file then the current code file has no knowledge of it.

 

Except when dealing with class libraries, these are preloaded at runtime and are accessable on any page.  it is in these libraries that all of php's in built functions reside, hence why you don't have to include or refference any of these files directly.  You can autoload your own libraries as well, but that's another topic.

Ok, that example is quite straightforward.

 

then i suppose wordpress works with these preloaded libraries because i constantly see functions and variables in scripts that are not initialized there and nor is there an include or require to files that contain these variables.

 

ill be digging in those libraries then :)

 

thanks

On the back of another thread with similar questions - ignore the word global, don't even think about coding with it.

I would agree that - in most situations - you should not use the global keyword to make a variable global. But, defining constants - which will be global - is not a bad practice. And, many variables are global by default. So, I would not say to ignore the 'word' global.

 

if i define a constant like define('constant'), php manuel says it's global,

 

does this mean that i can use of different files also? like, i defined in define.php and used in anotherfile.php, or shoud the latter file be included then?

When a user makes a request for a 'page' there may be any number of scripts that are executed to generate the page. Once a constant is defined any additional code that is run can reference that constant. It does not mean that you can define a constant on one page load and then have that constant available in subsequent page loads. It would need to be defined on any page request that you need it - and it does not retain it's value from previous page loads.

 

what about file

 

a.php includes file b.php which in turn includes file c.php. Can functions of a use variables of file c?

The easiest way to understand how includes work is this. Imagine that wherever there is an include (or require) that the system is copying the code from the include file and pasting it into the file that included it. Anything that occured in earlier files will affect any code in included files just as if they were in the same script.

 

what about variables that are declared global inside a function but this variables is not initialized in that script, like in wordpress:

 

function date_i18n( $dateformatstring, $unixtimestamp = false, $gmt = false ) {
   global $wp_locale;

// more code 

}

 

$wp_locale can be initialized in another script file?

 

Depends on what you mean. If that script/file is included() during the same execution that the function was run then, yes, it would be available. If it is a different page load, then no. But, as muddy_funster stated, this is considered poor practice and I agree.

 

You should read through this page in the manual: http://us3.php.net/manual/en/language.variables.scope.php

ok thanks for the asnwers,

 

about the keyword global: i've seen more people saying it's considered a bad practice. Why exactly?

 

I see it alot of times being used in functions in wordpress, are you guys suggesting that's bad software then? (neutrally said :))

 

 

 

 

 

 

 

 

about the keyword global: i've seen more people saying it's considered a bad practice. Why exactly?

 

It completely breaks the encapsulation of a function etc by introducing variables from the global scope..

 

Wordpress is poorly coded..

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.