PHP - 将变量传递给类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4877851/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP - passing variables to classes
提问by Alex
I trying to learn OOP and I've made this class
我正在尝试学习 OOP 并且我已经上了这门课
class boo{
function boo(&another_class, $some_normal_variable){
$some_normal_variable = $another_class->do_something();
}
function do_stuff(){
// how can I access '$another_class' and '$some_normal_variable' here?
return $another_class->get($some_normal_variable);
}
}
and I call this somewhere inside the another_class
class like
我在another_class
课堂内的某个地方称它为
$bla = new boo($bla, $foo);
echo $bla->do_stuff();
But I don't know how to access $bla, $foo inside the do_stuff function
但我不知道如何在 do_stuff 函数中访问 $bla, $foo
回答by Logan Bailey
<?php
class Boo
{
private $bar;
public function setBar( $value )
{
$this->bar = $value;
}
public function getValue()
{
return $this->bar;
}
}
$x = new Boo();
$x->setBar( 15 );
print 'Value of bar: ' . $x->getValue() . PHP_EOL;
Please don't pass by reference in PHP 5, there is no need for it and I've read it's actually slower.
请不要在 PHP 5 中通过引用传递,不需要它,而且我读过它实际上更慢。
I declared the variable in the class, though you don't have to do that.
我在类中声明了变量,但您不必这样做。
回答by ircmaxell
Ok, first off, use the newer style constructor __construct
instead of a method with the class name.
好的,首先,使用较新样式的构造函数__construct
而不是具有类名的方法。
class boo{
public function __construct($another_class, $some_normal_variable){
Second, to answer your specific question, you need to use member variables/properties:
其次,要回答您的具体问题,您需要使用成员变量/属性:
class boo {
protected $another_class = null;
protected $some_normal_variable = null;
public function __construct($another_class, $some_normal_variable){
$this->another_class = $another_class;
$this->some_normal_variable = $some_normal_variable;
}
function do_stuff(){
return $this->another_class->get($this->some_normal_variable);
}
}
Now, note that for member variables, inside of the class we reference them by prefixing them with $this->
. That's because the property is bound to thisinstance of the class. That's what you're looking for...
现在,请注意,对于成员变量,在类内部我们通过在它们前面加上前缀来引用它们$this->
。这是因为属性绑定到这个类的实例。这就是你要找的...
回答by Rafe Kettler
In PHP, constructors and destructors are written with special names (__construct()
and __destruct()
, respectively). Access instance variables using $this->
. Here's a rewrite of your class to use this:
在 PHP 中,构造函数和析构函数使用特殊名称(分别为__construct()
和__destruct()
)。使用$this->
. 这是您的类的重写以使用它:
class boo{
function __construct(&another_class, $some_normal_variable){
$this->another_class = $another_class;
$this->some_normal_variable = $another_class->do_something();
}
function do_stuff(){
// how can I access '$another_class' and '$some_normal_variable' here?
return $this->another_class->get($this->some_normal_variable);
}
}
回答by Foo Bah
You need to capture the values in the class using $this:
您需要使用 $this 捕获类中的值:
$this->foo = $some_normal_variable