If I have a function and the variables inside I want to be private to that function is this possible? If so how do I make them private or are they by default?
Printable View
If I have a function and the variables inside I want to be private to that function is this possible? If so how do I make them private or are they by default?
Do you mean functions in a calss? If so in PHP 4 there is no private/public but it has been introduced in PHP 5.
If you are talking about variable scope then this is how it works.
- All variables used inside a function have local scope.
- Local variables with the same name as global variables elsewhere in a script are treated as different entities with the exception of super globals
- To use a global variable declared outside a function you must either declar it inside the function using the global keyword or access it through the super global array $GLOBALS.
Here's some examples:
PHP Code:<?php
$global_var = 'no function called';
echo("Calling myfunction: ");
myfunction();
echo("\$global_var=$global_var");
echo("Calling myfunction2: ");
myfunction2();
echo("\$global_var=$global_var");
echo("Calling myfunction3: ");
myfunction3();
echo("\$global_var=$global_var");
function myfunction()
{
/* this creates a new variable with called - $global_var with local scope */
$global_var = 'myfunction called';
}
function myfunction2()
{
/* we can declare a global variable here */
global $global_var;
$global_var = 'myfunction2 called';
}
function myfunction3()
{
/* we can also use the $GLOBALS super global array */
$GLOBALS['global_var'] = 'myfunction3 called';
}
?>
The global thing isn't like extern (in C++) is it? Like in the following code I've highlighted two of them, are they using the same space? I'm doughting this but hopeing at the same time. Just cos I have variables that I might want to be global and also used in more than one function I guess to do that I'd have to use the super global.I think for now all I needed to know was that my function variables were private :D.PHP Code:<?php
$global_var = 'no function called'; <---- This one
echo("Calling myfunction: ");
myfunction();
echo("\$global_var=$global_var");
echo("Calling myfunction2: ");
myfunction2();
echo("\$global_var=$global_var");
echo("Calling myfunction3: ");
myfunction3();
echo("\$global_var=$global_var");
function myfunction()
{
/* this creates a new variable with called - $global_var with local scope */
$global_var = 'myfunction called';
}
function myfunction2()
{
/* we can declare a global variable here */
global $global_var;
$global_var = 'myfunction2 called'; <---- This one
}
function myfunction3()
{
/* we can also use the $GLOBALS super global array */
$GLOBALS['global_var'] = 'myfunction3 called';
}
?>
Yes ;)Quote:
Originally posted by Electroman
The global thing isn't like extern (in C++) is it?
I knew I shouldn't have asked a negative question :(. So it is like extern?Quote:
Posted by visualAd
Yes ;)
It is like extern, except that it is mandatory if you want to access a global. That's because of the lack of variable declarations in PHP.
Sorry - I misread. It is like extern.Quote:
Originally posted by Electroman
I knew I shouldn't have asked a negative question :(. So it is like extern?