Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP constructor with a parameter

I need a function that will do something like this:

$arr = array(); // This is the array where I'm storing data

$f = new MyRecord(); // I have __constructor in class Field() that sets some default values
$f->{'fid'} = 1;
$f->{'fvalue-string'} = $_POST['data'];
$arr[] = $f;

$f = new Field();
$f->{'fid'} = 2;
$f->{'fvalue-int'} = $_POST['data2'];
$arr[] = $f;

When I write something like this:

$f = new Field(1, 'fvalue-string', $_POST['data-string'], $arr);
$f = new Field(2, 'fvalue-int', $_POST['data-integer'], $arr);

// Description of parameters that I want to use:
// 1 - always integer, unique (fid property of MyRecord class)
// 'fvalue-int' - name of field/property in MyRecord class where the next parameter will go
// 3. Data for field specified in the previous parameter
// 4. Array where the class should go

I don’t know how to make a parametrized constructor in PHP.

Now I use a constructor like this:

class MyRecord
{
    function __construct() {
        $default = new stdClass();
        $default->{'fvalue-string'} = '';
        $default->{'fvalue-int'} = 0;
        $default->{'fvalue-float'} = 0;
        $default->{'fvalue-image'} = ' ';
        $default->{'fvalue-datetime'} = 0;
        $default->{'fvalue-boolean'} = false;

        $this = $default;
    }
}
like image 353
Kamil Avatar asked Feb 19 '12 04:02

Kamil


People also ask

Why we use __ construct in PHP?

PHP - The __construct Function A constructor allows you to initialize an object's properties upon creation of the object. If you create a __construct() function, PHP will automatically call this function when you create an object from a class. Notice that the construct function starts with two underscores (__)!

Do PHP classes need constructors?

PHP allows developers to declare constructor methods for classes. Classes which have a constructor method call this method on each newly-created object, so it is suitable for any initialization that the object may need before it is used.

What are the __ construct () and __ destruct () methods in a PHP class?

Example# __construct() is the most common magic method in PHP, because it is used to set up a class when it is initialized. The opposite of the __construct() method is the __destruct() method. This method is called when there are no more references to an object that you created or when you force its deletion.

How do you call a constructor in PHP?

To call the constructor of the parent class from the constructor of the child class, you use the parent::__construct(arguments) syntax. The syntax for calling the parent constructor is the same as a regular method.


2 Answers

Read all of Constructors and Destructors.

Constructors can take parameters like any other function or method in PHP:

class MyClass {

  public $param;

  public function __construct($param) {
    $this->param = $param;
  }
}

$myClass = new MyClass('foobar');
echo $myClass->param; // foobar

Your example of how you use constructors now won't even compile as you can't reassign $this.

Also, you don't need the curly brackets every time you access or set a property. $object->property works just fine. You only need to use curly-brackets under special circumstances like if you need to evaluate a method $object->{$foo->bar()} = 'test';

like image 81
Mike B Avatar answered Oct 05 '22 21:10

Mike B


If you want to pass an array as a parameter and 'auto' populate your properties:

class MyRecord {
    function __construct($parameters = array()) {
        foreach($parameters as $key => $value) {
            $this->$key = $value;
        }
    }
}

Notice that a constructor is used to create & initialize an object, therefore one may use $this to use / modify the object you're constructing.

like image 29
Michael Robinson Avatar answered Oct 05 '22 19:10

Michael Robinson