Jump to content

Regular old functions vs. Classes (Methods)?


Far Cry

Recommended Posts

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.

Link to comment
Share on other sites

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;
	}
}

}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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();

Link to comment
Share on other sites

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.

 

 

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.