Jump to content
MakeWebGames

[OOP] __construct() ????


Floydian

Recommended Posts

What is a __construct()?

Introduced into PHP 5 and it's vastly upgraded OOP implemenation are constructors and destructors.

Every time an object is created, the __construct() method is called. Every time an object is closed out by either being unset($object); or when the script execution is finished, the __destruct() method is called.

When creating a new object, arguments can be passed to the constructor. This can be a big benefit. We'll use a Database class to show how to pass arguments to a constructor.

 

<?php
$db = new Database(DB_USER, DB_PASS, DB_HOST, DB_DATABASE);
?>

 

That's it. What we've done here is we have four constants defined. These contain our database's user name, password, host, and the database we will be using.

If you are going to be using multiple databases, you can make a different object for each one. $db1, $db2, and so on.

The benefit of the constructor in this case is that connecting to the database is automatically handled when the object is created. The destructor will automatically close the database for us.

To close the database connection we use this: unset($db);

And the connection is closed. Since it's assumed we'll not be querying the database after closing the connection, we no longer need the object either, so we put those all in one. the __destruct() method does not accept any arguments.

The lack of constructors and destructors in PHP 4 is a seriously limiting aspect of PHP 4's implementation of OOP.

EDIT: A constructor can be simulated in PHP 4. I'll describe how to do this at the end of this post. I don't know of any way to simulate a destructor however.

You can call the __construct() and __destruct() methods at any time if you choose to do so.

$db->__construct();

$db->__destruct();

Calling the destructor does not close out the object. The destructor is simply intended to be code that is executed when an object is closed out, not as a way to close out an object.

I use a class in my game for controlling many aspects of users. The constructor method does things like checking to see if the player is online or not. It also does a basic query gathering information from the users table like the username, their cash on hand, and information needed for drawing user bars. There's much more that the class can do after that, but that's basically what the constructor does.

A page on my game would start with something like:

 

<?php
// I'm not passing anything to the constructor because the only information it needs
// is the user id of the player which is stored in a session, and since sessions
// are super globals, the $_SESSION['userid'] variable is available to the constructor as is.
$control = new Control();

if (!$control->isLoggedIn()) {
header(Location: blah blah); // redirect people that aren't logged in to the login page.
}
$energy = $control->userBar('energy'); // get a user bar that is drawn up for me automatically.
$nerve = $control->userBar('nerve ');
$brave = $control->userBar('brave ');

$my_name = $control->myName();

echo <<<EOT


$my_name</p>


$energy</p>


$nerve</p>


$brave</p>
EOT;
?>

 

Hopefully this demonstrates that the constructor handles the pre processing of quite a bit of data. Every other method I've called, uses information stored in properties that the __contruct function initializes and then returns that data in a meaningful format.

I've had to pass not even one variable to these methods. The userBar() method needs to know what kind of bar we're dealing with in case a bar as special formatting, like energy going up to 150% instead of 100%, or in the case of nerve, it's not a % at all, but a litteral number. And then the name of that bar is generated to match the bar, i.e., Nerve: 22, Energy: 125%.

When you look at my header code, you'll see that it's crazy how simple it looks. And it's because I've put all the mechanics into a class, and all of the presentation after it.

Enjoy ;)

 

Simulating a constructor in PHP 4:

What you do is make a function that has the same name as your class. That function will automatically be called when the class is instantiated.

 

<?php
class MyExample {
function MyExample($something) {
echo $something;
}
}

$something = "This is how you can simulate a constructor in PHP 4!";
$an_example = new MyExample($something);
?>
Link to comment
Share on other sites

Re: [OOP] __construct() ????

You're welcome!

I have to admit that being PHP 5 person, as in I learned to code using PHP 5, I did make a mistake with what I thought about PHP 4.

There is a way to simulate a constructor in PHP 4.

The correction is noted in the main post.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...