PHP's define("CONSTANT", "Hello world.");
is global function which is you can call it even if your inside the class or function. I have example below and let's say inside the class I declare a global variable supposed to be like private global $allForm
;
my question how can i call the $this->allForm inside validate() without embedding the $this->validate() inside the __construct?
$authentication = new authentication("1", "nanat"开发者_如何转开发, "amew", "yes" ); // declared define constant
class authentication extends mySession {
private $allForm; //variable
public function __construct($submit, $user, $password, $remmeber) {
$this->allForm = array('giSub' => $submit, 'giUser' => $user, 'giPas' => $password, 'giRemmeb' => $remmeber); //execute
// $this->validate(); //not execute
// $this->validateOne(); //not execute
// $this->validateTwo() //not execute
}
private function validate() {
var_dump($this->allForm); // return null
// statement...
}
private function validateOne() {
var_dump($this->allForm); // return null
// statement...
}
private function validateTwo() {
var_dump($this->allForm); // return null
// statement...
}
}
what i want is.. is this possible?
class authentication extends mySession {
private $allForm; //global variable..
public function __construct($submit, $user, $password, $remmeber) {
$this->allForm = array('giSub' => $submit, 'giUser' => $user, 'giPas' => $password, 'giRemmeb' => $remmeber); //execute
}
private function validate() {
var_dump($this->allForm); // return true
// statement...
}
private function validateOne() {
var_dump($this->allForm); // return true
// statement...
}
private function validateTwo() {
var_dump($this->allForm); // return true
// statement...
}
}
You aren't required to immediately call $this->validate()
from the construct. If you're populating $this->allForm
in the constructor, the value will continue to exist when you call class methods. Remove that $this->validate()
from __construct()
and you can call the validate()
method manually:
$authentication = new authentication( "1", "nanat", "amew", "yes" );
$authentication->validate();
Based on the way you have asked this question, that's the best answer I can provide.
$allForm
is declared as a private
member of class authentication
. This means that you can only access $allForm
from code that is inside class authentication
. If you want to access $allForm
from outside the class, you'll need to declare it as public
.
You can apply the same reasoning to the validate()
method as well. If you want to be able to call it from outside the class, then just declare it public
:
class authentication extends mySession {
public $allForm;
...
public function validate() {
...
}
}
Now you can do stuff like this....
$authentication = new authentication("1", "nanat", "amew","yes" );
$authentication->validate();
OR
$authentication->allForm = array(....);
精彩评论