PHP Classes and Objects Instantiating Objects - Web Development and Design | Tutorial for Java, PHP, HTML, Javascript PHP Classes and Objects Instantiating Objects - Web Development and Design | Tutorial for Java, PHP, HTML, Javascript

Breaking

Post Top Ad

Post Top Ad

Friday, May 24, 2019

PHP Classes and Objects Instantiating Objects

PHP Classes and Objects




Instantiating Objects

Problem

You want to create a new instance of an object.

Solution

Define the class, then use new to create an instance of the class:

          class user {
               function load_info($username) {
                     // load profile from database
               }
          }

          $user = new user;
          $user->load_info($_GET['username']);

Discussion

You can instantiate multiple instances of the same object:

            $adam = new user;
            $adam->load_info('adam');

            $dave = new user;
            $dave->load_info('adam');

These are two independent objects that happen to have identical information. They’re like identical twins; they may start off the same, but they go on to live separate lives.


No comments:

Post a Comment

Post Top Ad