In php when initializing a class how would one pas

2019-01-30 14:36发布

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条回答
神经病院院长
2楼-- · 2019-01-30 15:09

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

$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.

查看更多
家丑人穷心不美
3楼-- · 2019-01-30 15:09
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))

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

Person(array('isFat' => true, 'canRunFast' => false)) 
查看更多
Deceive 欺骗
4楼-- · 2019-01-30 15:09

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:

class myClass {

    var $myVar;

    function myClass { // PHP4 constructor, calls PHP5 constructor
        $this->__construct();
    }

    function __construct() { // PHP5 constructor
        doSomeStuff($myVar);
    }
}
查看更多
Emotional °昔
5楼-- · 2019-01-30 15:12

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.

查看更多
爷、活的狠高调
6楼-- · 2019-01-30 15:23

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

$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.

查看更多
Emotional °昔
7楼-- · 2019-01-30 15:24

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.

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.

查看更多
登录 后发表回答