Recipe 7.1. Instantiating Objects


7.1.1. Problem

You want to create a new instance of an object.

7.1.2. 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']);

7.1.3. 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.

7.1.4. See Also

Recipe 7.10 for more on copying and cloning objects; documentation on classes and objects at http://www.php.net/oop.




PHP Cookbook, 2nd Edition
PHP Cookbook: Solutions and Examples for PHP Programmers
ISBN: 0596101015
EAN: 2147483647
Year: 2006
Pages: 445

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net