bukwus Posted March 1, 2011 Share Posted March 1, 2011 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... Quote Link to comment https://forums.phpfreaks.com/topic/229324-teaching-myself-oop/ Share on other sites More sharing options...
trq Posted March 2, 2011 Share Posted March 2, 2011 Simply using classes is NOT OOP, especially when they (like this one) add little value to the design. Quote Link to comment https://forums.phpfreaks.com/topic/229324-teaching-myself-oop/#findComment-1181632 Share on other sites More sharing options...
Muddy_Funster Posted March 2, 2011 Share Posted March 2, 2011 PHP is OOP? Quote Link to comment https://forums.phpfreaks.com/topic/229324-teaching-myself-oop/#findComment-1181637 Share on other sites More sharing options...
bukwus Posted March 2, 2011 Author Share Posted March 2, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/229324-teaching-myself-oop/#findComment-1181811 Share on other sites More sharing options...
ManiacDan Posted March 2, 2011 Share Posted March 2, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/229324-teaching-myself-oop/#findComment-1181816 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.