开发者

OOP PHP classes

Let's say I have a class in php, and it includes some functions.

The class is named something.

When I load the file on another 开发者_如何学JAVAfile, I noticed it goes like:

include("the_file_with_the_class.php");
$something = new something(true);

now I can do OOP, I know, like $something->the_function, but what is that (true) in the variable? That really confused me a lot.


It's a constructor parameter.


In the example you gave:

$something = new something(true);

The true is a parameter being passed into the class constructor method.

If you're in PHP5, the constructor method will be named function __constructor(). It works just like any other function in that you can specify parameters for it, and these are passed in when you construct an object using new, as per your example.

So in your example, the class would have a parameter which (presumably) expects a boolean value and does something different when the object is initialised based on the value of that parameter.


It's an argument that is passed to the constructor

http://php.net/manual/en/language.oop5.decon.php


The true is a parameter that gets passed to the constructor of that class. The constructor is a "magic method" that is called upon - as the name says - construction of the object.

class myclass
 {
  function __construct($sunnyDay)
   {
    if ($sunnyDay) echo "It's a sunny day!";
   }
  }


  if ($temperature > 20)
   $myclass = new myclass(true); // Outputs "It's a sunny day"


According to your code, true is an argument to something's class constructor.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜