aim25 Posted July 10, 2008 Share Posted July 10, 2008 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 More sharing options...
trq Posted July 10, 2008 Share Posted July 10, 2008 1: No, variables declared within functions are only available within said functions. 2: No, to achieve that class B would need to extend class A. Link to comment https://forums.phpfreaks.com/topic/114026-solved-scopes-in-php/#findComment-586039 Share on other sites More sharing options...
aim25 Posted July 10, 2008 Author Share Posted July 10, 2008 TY, that clears makes things a lot more clear. Link to comment https://forums.phpfreaks.com/topic/114026-solved-scopes-in-php/#findComment-586042 Share on other sites More sharing options...
btherl Posted July 10, 2008 Share Posted July 10, 2008 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.