phpclass

Is it possible to initialize a class with parameters?


I want to do something like this:

$obj = new myClass('myID');

I've tried but it gave me an error. If this is possible, how can I do it properly?


Solution

  • Yes it is, you need to pass the variables through the constructor:

    class SomeClass
    {
        function __construct($some_var)
        {
        }
    }
    

    Please note that in older versions of php the constructor needs to have the name of the class, it´s __construct() since php 5.