Far Cry Posted October 11, 2011 Share Posted October 11, 2011 I have recently been looking into classes and objects, and have really not been clear what the difference between just using functions or just using classes. Also, what's the point of using properties inside classes if you can pass variables to the function like you would normally? Thanks in advance. Quote Link to comment Share on other sites More sharing options...
requinix Posted October 11, 2011 Share Posted October 11, 2011 If you're only using static methods inside classes then there's no point and you can just use regular functions. But if not, and you end up writing code like $user = create_user("name", "email@example.com"); add_user_metadata($user, "source", "website"); save_user($user); send_user_email($user, format_user_email_message("welcome", $user)); log_user_event($user, "sent welcome message"); (in other words, passing $user around to a bunch of functions) then you can use objects instead like $user = new User("name", "email@example.com"); $user->metadata["source"] = "website"; $user->save(); $user->send_email($user->format_email_message("welcome")); $user->log_event("sent welcome message"); But guess what? If you just want to use regular functions then just use regular functions. Properties have two main purposes: 1. They're variables. echo $user->email; // email@example.com 2. They can track extra information that other code doesn't need to care about. class User { private $_dirty = false; public function save() { if ($this->_dirty) { // save... $this->_dirty = false; } } } Quote Link to comment Share on other sites More sharing options...
markjoe Posted October 11, 2011 Share Posted October 11, 2011 Object Oriented Programming (classes/methods) aka OOP and functional programming can each perform the same tasks. You won't gain some amazing functionality using one or the other. One or the other may be better suited for a specific task, however. OOP can usually save you some typing by re-using more code, however, many "proper" OOP designs will actually be more work upfront (but save time later). PHP OOP is a bit of an odd thing being that it is a "bolt-on". I would strongly recommend to keep to one or the other style when developing in PHP. Having functional and OOP styles mixed in the same files will turn into a nightmare. Quote Link to comment Share on other sites More sharing options...
requinix Posted October 11, 2011 Share Posted October 11, 2011 Having functional and OOP styles mixed in the same files will turn into a nightmare. That will be unavoidable since 80% of PHP is procedural anyways. My advice is to use objects when they make sense and functions when they don't. Quote Link to comment Share on other sites More sharing options...
markjoe Posted October 11, 2011 Share Posted October 11, 2011 My point is don't do this: <?php require('header.php'); // where header.php prints out html code or whatever class Item{ function Item($name){ $this->name = $name; } } foreach($some_array as $element){ $i = new Item($element); echo $i->name; } Yes, you will always have some mixture since PHP is not 100% OOP (insert Ruby plug here), but the very least you should do is keep your class definitions separate. Quote Link to comment Share on other sites More sharing options...
requinix Posted October 11, 2011 Share Posted October 11, 2011 Oh, mixing them like the definitions and the usage in the same file. Yeah I'm totally with you there. Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted October 12, 2011 Share Posted October 12, 2011 Now, this class can easily be reused! Basically it is an example social network news feed. Writing the class takes the longest, but then when you want to get some members news feed, you just need to use 1-2 lines of code and POOF! a news feed! require_once "db.php"; class Member{ private $id = null; private $name = null; private $email = null; private $db = null; private $friends = array(); public function __construct($member_id){ $member_id = (int)$member_id; $db = new dbConnect(); $db->query("select * from members where member_id = $member_id"); $this->id = $db->row->member_id; $this->name = $db->row->name; $this->email = $db->row->email; } public function getNewsFeed(){ $this->getfriends(); $friends = implode(",", array_keys($this->friends)); $db->query("select * from news_feed where poster in($friends)"); while($db->row()){ return "<p>$db->row->message</p>"; } } public function getfriends(){ if(count($this->friends) > 0) return true; $db->query("select f.member_id, concat(m.first_name, ' ', m.last_name) full_name from friends f left join members m on (f.member_id = m.member_id) where owner = $this->member_id"); while($db->row()){ $this->friends[$db->row->member_id] = $db->row->full_name; } return true; } } then, its usage would look like this: require_once 'classes/Member.php'; $member = new Member(12312312); echo $member->getNewsFeed(); Quote Link to comment Share on other sites More sharing options...
jpcicero Posted October 12, 2011 Share Posted October 12, 2011 OOP vs Regular Old Functions, it is a huge topic, these are two different methodologies to face one problem. I can imagine that you have experience in procedural methodology but you need some OO background, if not, sorry for this reply, you will found thousands of oo tutorials on the web. Basically OO approach will well you to: 1. Ease analysis, you always think your problem using objects (Customer, Invoice, Bank Account, are all objects). What these object does?, well, these are their methods, Customer->buyItem($item). 2. Reuse your code, it's easier to call an object customer, every where inside one or more applications. 3. Less errors, if you have a well designed object, with a clear interface (how it communicate with the context), will works always (theoretically ) 4. You can use unit testing. 5. Inheritance and composition they are a huge concepts that will give you a lot of benefits, for instance, when you need to create a framework. There is a lot of benefits by using OO approach, will be so extensive to explain here, my intention it's only to encourage you to use it. Quote Link to comment 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.