So here is the deal. I want to call a class and pass a value to it so it can be used inside that class in all the various functions ect. ect. How would I go about doing that?
所以这里是交易。我想调用一个类并将值传递给它,以便它可以在所有各种函数中的该类中使用。等。我该怎么做呢?
Thanks, Sam
谢谢,山姆
7 个解决方案
#1
51
I want to call a class and pass a value to it so it can be used inside that class
我想调用一个类并将值传递给它,以便它可以在该类中使用
The concept is called "constructor".
这个概念被称为“构造函数”。
As the other answers point out, you should use the unified constructor syntax (__construct()
) as of PHP 5. Here is an example of how this looks like:
正如其他答案所指出的那样,您应该使用PHP 5中的统一构造函数语法(__construct())。以下是一个示例:
class Foo {
function __construct($init_parameter) {
$this->some_parameter = $init_parameter;
}
}
// in code:
$foo = new Foo("some init value");
Notice - There are so-called old style constructors that you might run into in legacy code. They look like this:
注意 - 您可能会在遗留代码中遇到所谓的旧样式构造函数。它们看起来像这样:
class Foo {
function Foo($init_parameter) {
$this->some_parameter = $init_parameter;
}
}
This form is officially deprecated as of PHP 7 and you should no longer use it for new code.
从PHP 7开始,此表单已正式弃用,您不应再将其用于新代码。
#2
33
In new versions of PHP (5 and up), the function __constuct is called whenever you use "new {Object}", so if you want to pass data into the object, add parameters to the construct function and then call
在新版本的PHP(5及更高版本)中,每当使用“new {Object}”时都会调用函数__constuct,因此如果要将数据传递给对象,请将参数添加到构造函数中,然后调用
$obj = new Object($some, $parameters);
class Object {
function __construct($one, $two) {}
}
Named constructors are being phased out of PHP in favor of the __construct method.
命名构造函数正逐步退出PHP,转而使用__construct方法。
#3
15
class SomeClass
{
public $someVar;
public $otherVar;
public function __construct()
{
$arguments = func_get_args();
if(!empty($arguments))
foreach($arguments[0] as $key => $property)
if(property_exists($this, $key))
$this->{$key} = $property;
}
}
$someClass = new SomeClass(array('someVar' => 'blah', 'otherVar' => 'blahblah'));
print $someClass->someVar;
This means less maintenance in the long run.
这意味着从长远来看减少维护。
Order of passed variables is not important anymore, (no more writing defaults like 'null': someClass(null, null, true, false))
传递的变量的顺序不再重要,(不再编写像'null'这样的默认值:someClass(null,null,true,false))
Adding a new variable is less hassle (don't have to write the assignment in the constructor)
添加新变量不那么麻烦(不必在构造函数中编写赋值)
When you look at the instantiation of the class you'll know immediately what the passed in variables relate to:
当您查看类的实例化时,您将立即知道传入的变量与哪些相关:
Person(null, null, true, false)
vs
VS
Person(array('isFat' => true, 'canRunFast' => false))
#4
12
This is how I do mine
这就是我的做法
class MyClass {
public variable; //just declaring my variables first (becomes 5)
public variable2; //the constructor will assign values to these(becomes 6)
function __construct($x, $y) {
$this->variable = $x;
$this->variable2 = $y;
}
function add() {
$sum = $this->variable + $this->variable2
return $sum;
}
} //end of MyClass class
Create an instance and then call the function add
创建一个实例,然后调用函数add
$myob = new MyClass(5, 6); //pass value to the construct function
echo $myob->add();
11 will be written to the page not a very useful example because you would prefer to pass value to add when you called it but this illustrates the point.
11将写入页面不是一个非常有用的例子,因为你更喜欢在调用时传递值来添加,但这说明了这一点。
#5
3
You can do this like that:
你可以这样做:
class SomeClass
{
var $someVar;
function SomeClass($yourValue)
{
$this->someVar = $yourValue;
}
function SomeFunction()
{
return 2 * $this->someVar;
}
}
or you can use __construct instead of SomeClass for constructor in php5.
或者您可以在php5中使用__construct而不是SomeClass作为构造函数。
#6
0
Think everyone's missing the obvious here. Yes, the PHP4 constructor is deprecated, but you can write your class to be backwards compatible like this:
认为每个人都错过了这里的明显。是的,不推荐使用PHP4构造函数,但是您可以将类编写为向后兼容,如下所示:
class myClass {
var $myVar;
function myClass { // PHP4 constructor, calls PHP5 constructor
$this->__construct();
}
function __construct() { // PHP5 constructor
doSomeStuff($myVar);
}
}
#7
-2
Wow I cannot believe the answers here! They will all work but are wrong, the right way of set the variables is by a getter and setter, this allows you to set your variable neatly and perform checks etc on them. e.g.
哇我无法相信这里的答案!它们都可以工作但是错误,设置变量的正确方法是通过getter和setter,这允许你整齐地设置变量并对它们执行检查等。例如
class myClass {
private $myVar;
public function set_var($var) { // You can then perform check on the data etc here
$this->myVar = $var;
}
function __construct() { // PHP5 constructor
}
public function do_something() {
echo "blah";
}
}
What this allows you to do is call the object correctly e.g.
这允许你做的是正确调用对象,例如
$objNew = new myClass();
$objNew->set_var("Set my variable");
$objNew->do_something();
This is the tidy way of doing it and on large projects and scripts you will be glad of this, I am having this problem right now with some-else's script which cannot be updated easily because it has been written in the other ways mentioned in this page.
这是这样做的整洁方式,对于大型项目和脚本,你会很高兴,我现在遇到这个问题,其他的脚本无法轻易更新,因为它是用其他方式编写的。页。
It also allows you to have an unlimited number of variables for the class with out a silly call to the object e.g.
它还允许您为该类提供无限数量的变量,而不会对该对象进行愚蠢的调用,例如
$objNew = new myClass("var1","var1","var1","var1","var1","var1","var1","var1","var1","var1","var1","var1","var1","var1","var1","var1","var1");
This is basically no clearer than using a function.
这基本上不比使用函数更清晰。
#1
51
I want to call a class and pass a value to it so it can be used inside that class
我想调用一个类并将值传递给它,以便它可以在该类中使用
The concept is called "constructor".
这个概念被称为“构造函数”。
As the other answers point out, you should use the unified constructor syntax (__construct()
) as of PHP 5. Here is an example of how this looks like:
正如其他答案所指出的那样,您应该使用PHP 5中的统一构造函数语法(__construct())。以下是一个示例:
class Foo {
function __construct($init_parameter) {
$this->some_parameter = $init_parameter;
}
}
// in code:
$foo = new Foo("some init value");
Notice - There are so-called old style constructors that you might run into in legacy code. They look like this:
注意 - 您可能会在遗留代码中遇到所谓的旧样式构造函数。它们看起来像这样:
class Foo {
function Foo($init_parameter) {
$this->some_parameter = $init_parameter;
}
}
This form is officially deprecated as of PHP 7 and you should no longer use it for new code.
从PHP 7开始,此表单已正式弃用,您不应再将其用于新代码。
#2
33
In new versions of PHP (5 and up), the function __constuct is called whenever you use "new {Object}", so if you want to pass data into the object, add parameters to the construct function and then call
在新版本的PHP(5及更高版本)中,每当使用“new {Object}”时都会调用函数__constuct,因此如果要将数据传递给对象,请将参数添加到构造函数中,然后调用
$obj = new Object($some, $parameters);
class Object {
function __construct($one, $two) {}
}
Named constructors are being phased out of PHP in favor of the __construct method.
命名构造函数正逐步退出PHP,转而使用__construct方法。
#3
15
class SomeClass
{
public $someVar;
public $otherVar;
public function __construct()
{
$arguments = func_get_args();
if(!empty($arguments))
foreach($arguments[0] as $key => $property)
if(property_exists($this, $key))
$this->{$key} = $property;
}
}
$someClass = new SomeClass(array('someVar' => 'blah', 'otherVar' => 'blahblah'));
print $someClass->someVar;
This means less maintenance in the long run.
这意味着从长远来看减少维护。
Order of passed variables is not important anymore, (no more writing defaults like 'null': someClass(null, null, true, false))
传递的变量的顺序不再重要,(不再编写像'null'这样的默认值:someClass(null,null,true,false))
Adding a new variable is less hassle (don't have to write the assignment in the constructor)
添加新变量不那么麻烦(不必在构造函数中编写赋值)
When you look at the instantiation of the class you'll know immediately what the passed in variables relate to:
当您查看类的实例化时,您将立即知道传入的变量与哪些相关:
Person(null, null, true, false)
vs
VS
Person(array('isFat' => true, 'canRunFast' => false))
#4
12
This is how I do mine
这就是我的做法
class MyClass {
public variable; //just declaring my variables first (becomes 5)
public variable2; //the constructor will assign values to these(becomes 6)
function __construct($x, $y) {
$this->variable = $x;
$this->variable2 = $y;
}
function add() {
$sum = $this->variable + $this->variable2
return $sum;
}
} //end of MyClass class
Create an instance and then call the function add
创建一个实例,然后调用函数add
$myob = new MyClass(5, 6); //pass value to the construct function
echo $myob->add();
11 will be written to the page not a very useful example because you would prefer to pass value to add when you called it but this illustrates the point.
11将写入页面不是一个非常有用的例子,因为你更喜欢在调用时传递值来添加,但这说明了这一点。
#5
3
You can do this like that:
你可以这样做:
class SomeClass
{
var $someVar;
function SomeClass($yourValue)
{
$this->someVar = $yourValue;
}
function SomeFunction()
{
return 2 * $this->someVar;
}
}
or you can use __construct instead of SomeClass for constructor in php5.
或者您可以在php5中使用__construct而不是SomeClass作为构造函数。
#6
0
Think everyone's missing the obvious here. Yes, the PHP4 constructor is deprecated, but you can write your class to be backwards compatible like this:
认为每个人都错过了这里的明显。是的,不推荐使用PHP4构造函数,但是您可以将类编写为向后兼容,如下所示:
class myClass {
var $myVar;
function myClass { // PHP4 constructor, calls PHP5 constructor
$this->__construct();
}
function __construct() { // PHP5 constructor
doSomeStuff($myVar);
}
}
#7
-2
Wow I cannot believe the answers here! They will all work but are wrong, the right way of set the variables is by a getter and setter, this allows you to set your variable neatly and perform checks etc on them. e.g.
哇我无法相信这里的答案!它们都可以工作但是错误,设置变量的正确方法是通过getter和setter,这允许你整齐地设置变量并对它们执行检查等。例如
class myClass {
private $myVar;
public function set_var($var) { // You can then perform check on the data etc here
$this->myVar = $var;
}
function __construct() { // PHP5 constructor
}
public function do_something() {
echo "blah";
}
}
What this allows you to do is call the object correctly e.g.
这允许你做的是正确调用对象,例如
$objNew = new myClass();
$objNew->set_var("Set my variable");
$objNew->do_something();
This is the tidy way of doing it and on large projects and scripts you will be glad of this, I am having this problem right now with some-else's script which cannot be updated easily because it has been written in the other ways mentioned in this page.
这是这样做的整洁方式,对于大型项目和脚本,你会很高兴,我现在遇到这个问题,其他的脚本无法轻易更新,因为它是用其他方式编写的。页。
It also allows you to have an unlimited number of variables for the class with out a silly call to the object e.g.
它还允许您为该类提供无限数量的变量,而不会对该对象进行愚蠢的调用,例如
$objNew = new myClass("var1","var1","var1","var1","var1","var1","var1","var1","var1","var1","var1","var1","var1","var1","var1","var1","var1");
This is basically no clearer than using a function.
这基本上不比使用函数更清晰。