views:

38

answers:

5

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 file, 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.

+2  A: 

It's a constructor parameter.

JRL
A: 

According to your code true is an argument to "something"s class contructor

Stormherz
A: 

It's an argument that is passed to the constructor

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

Galen
A: 

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"
Pekka
A: 

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.

Spudley