cheetahten Posted June 23, 2009 Share Posted June 23, 2009 I have this tiny Project on the Web that I do as a learning tool. It is running fine in PHP5. Most of all PHP in it is written from Scratch I been thinking of rewriting it to be more OOP. I have read and reread about OOP/PHP. But am having a hard time figuring out when to write a function in OOP. OR Even how to get started with OOP on it. Basically am lost :-\ Quote Link to comment https://forums.phpfreaks.com/topic/163418-oop-php-am-having-a-hard-time-with-it/ Share on other sites More sharing options...
flyhoney Posted June 23, 2009 Share Posted June 23, 2009 Obviously the internet is your friend. Just keep googling. You're on the right track though, learning by doing is the best way. Remember, it's called Object Oriented for a reason. Use objects whenever you have a clearly defined object you need to represent. Often times, its better to write fake code that uses an object before you actually write the class for the object. For example, its very useful to have a Database object in most web applications. So lets pretend we already have one and explore how we would use it. <?php // Create the database object using the class constructor to connect to the database $database = new Database('hostname', 'username', 'password', 'database'); // Perform a query on the database. Return the query results as objects too! $flying_dinosaurs = $database->query("SELECT name FROM dinosaurs WHERE flying = TRUE"); // Iterate over the query results foreach ($flying_dinosaur as $dinosaur) { echo $dinosaur->name; } Isn't that code amazingly readable! Now we know exactly what our class needs to do and we can create it. <?php class Database { private $connection = NULL; public function __construct($hostname, $username, $password, $database) { $this->connection = mysql_connect($hostname, $username, $password); if ($this->connection) { mysql_select_db($database); } } public function query($query) { $result = mysql_query($query); $data = array(); while ($row = mysql_fetch_object($result)) { $data[] = $row; } return $data; } } This is just a simple example, but hopefully it helps shed light on OOP. Quote Link to comment https://forums.phpfreaks.com/topic/163418-oop-php-am-having-a-hard-time-with-it/#findComment-862316 Share on other sites More sharing options...
KevinM1 Posted June 24, 2009 Share Posted June 24, 2009 I have this tiny Project on the Web that I do as a learning tool. It is running fine in PHP5. Most of all PHP in it is written from Scratch I been thinking of rewriting it to be more OOP. I have read and reread about OOP/PHP. But am having a hard time figuring out when to write a function in OOP. OR Even how to get started with OOP on it. Basically am lost :-\ It's hard to suggest a particular way to go without knowing your current skill level. Then again, I get the feeling that you're in one of those pesky "I don't even know what I don't understand" phases. I'd say start with the idea of encapsulating your data. Each object should be essentially self-contained - they shouldn't know about the 'main' script, and they should only be able to access other objects' public interfaces (read: public functions). Keep each object as simple as possible. Every object you create should have a specific job. That job itself may be complicated, but you shouldn't overload objects with multiple tasks. You should be able to describe an object in a sentence - "This object does X." If you use the word 'and' more than, say, twice in that sentence, consider breaking the object down into smaller parts. Example: I'm currently writing a web game in another language. I have an object of type CombatEngine. It acts as a facade for the underlying combat system and maintains the overall combat state for the current encounter. Despite the object's internal complexity, I can describe it easily in a sentence. Of course, if you have specific questions, about either theory or code, don't hesitate to ask. Quote Link to comment https://forums.phpfreaks.com/topic/163418-oop-php-am-having-a-hard-time-with-it/#findComment-862369 Share on other sites More sharing options...
cheetahten Posted June 25, 2009 Author Share Posted June 25, 2009 Me again.... I do not think I am a total newbie. I come from a mainframe background...ugh...COBOL Anyways.. my tiny Web project is http://www.tanela.com..... a directory, well, at least it started as a directory, been getting bigger over the years. Of late I been working on letting a user to create a profile. With image uploads and dynamic resizing and bla bla...now that is a never ending monster. the whole thing is PHP. Am kinda Anti javascript and anti-flash. most js in site is for tinymce. A site should work with no javascript I believe, once it is working, then maybe it can be used to enhance existing functions Anyways, I been thinking of OOP PHP for the Database piece, and if I ever get the concept down in MY HEAD then maybe I'll tackle the login page and login functions I do alot of Validations, lets say... I wanna validate string, or a number. Would each of this valdation be a class? Quote Link to comment https://forums.phpfreaks.com/topic/163418-oop-php-am-having-a-hard-time-with-it/#findComment-863158 Share on other sites More sharing options...
KevinM1 Posted June 25, 2009 Share Posted June 25, 2009 I do alot of Validations, lets say... I wanna validate string, or a number. Would each of this valdation be a class? It could be done that way. I think, though, it'd be better to structure your validation based on the function of what the incoming data is supposed to be, rather than merely it's datatype. Example: an e-mail address is a string, but it must conform to other rules as well (such as requiring an '@' symbol somewhere). Here's an example of one solution to OOP validation: http://www.phpfreaks.com/forums/index.php/topic,188111.msg845281.html#msg845281 Quote Link to comment https://forums.phpfreaks.com/topic/163418-oop-php-am-having-a-hard-time-with-it/#findComment-863295 Share on other sites More sharing options...
dzelenika Posted June 25, 2009 Share Posted June 25, 2009 I have this tiny Project on the Web that I do as a learning tool. It is running fine in PHP5. Most of all PHP in it is written from Scratch I been thinking of rewriting it to be more OOP. I have read and reread about OOP/PHP. But am having a hard time figuring out when to write a function in OOP. OR Even how to get started with OOP on it. Basically am lost :-\ You will now, what you need when you start to work or make plains. I had the same problem. I thought about problems in past and their solutions. My guidelines are now DAO (data access objects), XSL(Extensible Stylesheet Language)/XSLT, MVC (model view controller), PDO ... They drive you to use OOP functionality in right way. There's no need to push OOP everywhere. Quote Link to comment https://forums.phpfreaks.com/topic/163418-oop-php-am-having-a-hard-time-with-it/#findComment-863330 Share on other sites More sharing options...
MasterACE14 Posted June 25, 2009 Share Posted June 25, 2009 http://en.wikipedia.org/wiki/Object_oriented An object-oriented program may thus be viewed as a collection of cooperating objects' date=' as opposed to the conventional model, in which a program is seen as a list of tasks (subroutines) to perform. In OOP, each object is capable of receiving messages, processing data, and sending messages to other objects and can be viewed as an independent 'machine' with a distinct role or responsibility. The actions (or "operators") on these objects are closely associated with the object. For example, the data structures tend to carry their own operators around with them (or at least "inherit" them from a similar object or class).[/quote'] Quote Link to comment https://forums.phpfreaks.com/topic/163418-oop-php-am-having-a-hard-time-with-it/#findComment-863336 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.