Jump to content

[SOLVED] Scopes in PHP


aim25

Recommended Posts

My script has many classes and functions. After a while of coding i came up with a few questions.

 

1. If i have a variable named $temp in function A(), is it the same as the $temp in function B()?

 

class SOMETHING {

function A() {
$temp;
}

function B() {
$temp;
}

}

 

2. Lets say i have 2 classes (class A, class B) which both extend from class MAIN. In MAIN there is a variable declared; $temp. If the value of $temp is changed in class A, then accessed in class B, is the value passed between the classes?

 

class MAIN {

var $temp;

}

class A extends MAIN {

$this->temp = 4;

}

class B extends MAIN {

echo $this->temp;

}

Link to comment
https://forums.phpfreaks.com/topic/114026-solved-scopes-in-php/
Share on other sites

To add to 2, a class definition is just a definition.  You can't set a variable in a definition of a class (except to set a default value).  You can only set a variable in an instance of a class.

 

If you have an instance of class A and an instance of class B, then their variables are totally separate, even if their classes both extended the same parent.

Link to comment
https://forums.phpfreaks.com/topic/114026-solved-scopes-in-php/#findComment-586046
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.