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
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
Share on other sites

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.