Jump to content

Teaching myself OOP


bukwus

Recommended Posts

Here's what I've been playing around with just to get to know OOP a little better. I created a PHP file that allows you to create a visible table for each time you click on the "Create Table" button. It works fine, but I want to know if I should be putting more of this code inside the CLASS. Here's the code:

<?php session_start();
class myClass {
	public $html = '<table width="50%" border="1">
		<tr>
			<td width="50%" border="1">
				Cell 1
			</td>

			<td width="50%" border="1">
				Cell 2
			</td>
		</tr>
	</table>';
}

if ((!isset($_POST['reset'])) && (!isset($_POST['createTable']))) {
	$_SESSION['tableCount'] = 0;
}

if (isset($_POST['reset'])) {
	$_SESSION['tableCount'] = 0;
	unset ($_POST['reset']);
}

if (isset($_POST['createTable'])) {
	$_SESSION['tableCount'] = $_SESSION['tableCount'] + 1;
	unset ($_POST['createTable']);
}

if ($_SESSION['tableCount'] > 0) {
	for ($i = 1; $i <= $_SESSION['tableCount']; $i++) {
		$obj[$i] = new myClass;
		echo $obj[$i] -> html;
	}
}
?>

<form action="class_lib2.php" method="post">
<p><input type="submit" name="createTable" value="Create Table" /></p>

<p><input type="submit" name="reset" value="Reset" /></p>
</form>

<?php echo $_SESSION['tableCount']; ?>

 

Any suggestions are welcome.

Thanks...

Link to comment
Share on other sites

Real helpful, guys. Like I said, I'm just starting out and experimenting. My intention was to allow someone to create multiple instances via a button. Once I understood this step in the process I would move on and add more functionality.

Link to comment
Share on other sites

What they're trying to say here is: You haven't done anything OOP.  You've changed $html into $class->html.  All that's done is add memory and processor overhead.  Yes, technically, you've used a class, but you haven't done anything particularly handy with it.

 

Classes are designed to be tools that hold their own data and functionality.  The general idea is that you can take a class from one project, copy the class file into another project, and use the class in the second project exactly like it was used in the first.  Classes are modular and designed to be self-contained.  Classes are also very mutable, they change often and rely on inputs to their constructors and functions.

 

Not to take you off this forum, but I wrote three good posts about OOP in PHP5 a few years back.  Not much has changed in PHP since then because PHP6 was pushed back.

 

-Dan

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

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

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.