Naming method inside of a Class

In MVC OOP  I am getting this warning:
Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; Data_model has a deprecated constructor in

Here is the class in my file data_model.php
Class Data_model extends CI_Model {

    function data_model() {
        $this->load->database();
        $this->table_name = 'data';
    }

Open in new window


How would I call how would you do it?
LVL 8
rgranlundAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gr8gonzoConsultantCommented:
Change:
function data_model() {
to
function __construct() {
Julian HansenCommented:
You can read more about this here
http://php.net/manual/en/migration70.deprecated.php
Chris StanyonWebDevCommented:
In older versions of PHP (pre 5 I think), the constructor (ctor) of a class was basically a function with the same name as the class:

class MyClass {
    public function MyClass()
    {
        // this is the CTOR and get's called whenever you create an instance of the class
    }
}

Open in new window

In your code, you have a function named the same as the class, so PHP is interpreting that to think you're creating a ctor, and it's telling you that way of doing things is outdated. The new way of creating a ctor is to use a function called __construct():

class MyClass {
    public function __construct()
    {
        // this is the CTOR and get's called whenever you create an instance of the class
    }
}

Open in new window

If you want the method to be a ctor, rename it to __construct(). If you want it to be a normal method, rename it to something other than the class name

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rgranlundAuthor Commented:
Thank you for explaining.  It makes sense now.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.