Ale*_*lex 4 php variables class function
我可以从类外部更改类中定义的函数或变量,但不使用全局变量吗?
这是类,里面包含文件#2:
class moo{
function whatever(){
$somestuff = "....";
return $somestuff; // <- is it possible to change this from "include file #1"
}
}
Run Code Online (Sandbox Code Playgroud)
在主应用程序中,这是使用类的方式:
include "file1.php";
include "file2.php"; // <- this is where the class above is defined
$what = $moo::whatever()
...
Run Code Online (Sandbox Code Playgroud)
您是在询问Getters和Setter还是静态变量?
class moo{
// Declare class variable
public $somestuff = false;
// Declare static class variable, this will be the same for all class
// instances
public static $myStatic = false;
// Setter for class variable
function setSomething($s)
{
$this->somestuff = $s;
return true;
}
// Getter for class variable
function getSomething($s)
{
return $this->somestuff;
}
}
moo::$myStatic = "Bar";
$moo = new moo();
$moo->setSomething("Foo");
// This will echo "Foo";
echo $moo->getSomething();
// This will echo "Bar"
echo moo::$myStatic;
// So will this
echo $moo::$myStatic;
Run Code Online (Sandbox Code Playgroud)