Jump to content

When would OOP be used


plznty

Recommended Posts

Hello fellow PHP freaks, I never really looked into OOP PHP code.

I've gone through tutorials and I get how it works since I've used it a few times now.

However it obviously is more heavy, since it has to import classes and functions etc into all the documents which is a fairly large overhead.

Where is the best place to use OOP? Like what site functions are better to use OOP rather than procedural.

Thanks in advance, any help would be a help, thanks.

Link to comment
Share on other sites

OOPHP is an approach of "include and reuse". Objects simplify web development by eliminating the need of cut, paste and adopt existing code. So as far as principle of programing languages is considered OOPHP will provide us some advanced features of php like reusability inheritance etc etc.

 

Speaking Specifically,

One of the new classes included in OOPHP like SimpleXMLElement class. With this class you can incorporate RSS feeds into a web page using very few lines of code. Additionally without a minimal understanding of the masics of OOPHP, you can use full use of the capabilities of PHP5. For instance, If you want to create SOAP client, there is a no other way to do it than by using the SOAPClient class. There is a no need that once you start programming using OO approach you need to always code in this way. OOPHP approach can be used when you want because we can use FULL functional PHP5 by this approach.

Link to comment
Share on other sites

Its when you want to group many similar functions into one item, then the functions can share properties (variables basically)

 

so, say you have a "Bank" class for a game or something, it would handle all the banking stuff, such as:

- Current Balance

- Deposits

- Withdrawals

- etc.

 

<?php
class Bank{
private $amount = 0.00;
private $balance = 0.00;
private $member_id = 0;
public function __construct($member_id){
	$this->member_id = (int)$member_id;
	$this->getBalance();
}
public function __get($name){
	if($name == 'amount') return $this->amount;
	if($name == 'balance') return $this->balance;
}
private function getBalance(){
	$sql = mysql_query("select sum(*) as balance, direction from transactions where member_id = $this->member_id group by direction");
	$array = array();
	while($row = mysql_fetch_assoc($sql)){
		$array[$row['direction']] = $row['balance'];
	}
	$this->balance = ((float)$array['in'] - (float)$array['out']);
	return $this->balance;
}
public function makePayment($amount, $direction = 'in'){
	$this->amount = (float)$amount;
	if($direction != 'in' || $direction != 'out')
		return false;
	if(mysql_query("insert into transactions (member_id, amount, direction) values ($this->member_id, $amount, '$direction')")){
		$this->getBalance();
		return true;
	}
	return false;
}
}
?>

 

now to use this all you need to do is this:

<?php
// Say the users current balance is $100 right now
require_once 'includes/Bank.php';  // our bank class
$bank = new Bank($_SESSION['member_id']);
echo "Balance: $$bank->balance";	// Prints 100
$bank->makePayment(100);
echo "<br>New Balance $$bank->balance";	// Prints 200
$bank->makePayment(25, 'out');
echo "<br>New Balance $$bank->balance";	// Prints 175
?>

 

It makes doing repetitive tasks MUCH easier to do.

 

Also, see how I use variables in different functions using $this, it can help to also remove parameters that you would have to pas in with normal functions. in some of the functions I would have passed member_id many times, but since it is a property it can be used within my methods many times with out me having to pass it to the method when I want to use it (if that makes sense).

Link to comment
Share on other sites

I use it for reusing blocks of code, i love it.

 

saves time and makes my code much easier to understand.

 

another benefit of OOP is that when you make an edit, only that block of code is affected and the site remains still generally usable. This means you can have a team of coders working on the same site without much conflicts. That is the most modern approach.

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.