Jump to content

OOP in PHP using Mysqli connection


albaserver

Recommended Posts

Hi,

I'm trying to write a better PHP code to create and manage my website.

 

I would like to start a MVC approach with PHP, using OOP. So I can manage the updates in a better way. For example, to begin my project, I would try with the shipping cost of our products, using a Class without merge the PHP and HTML code.

 

Something like this:

$shippingcost=new ShippingCost();
$shippingcost->state="Italy";
 
$shippingcost->get(); // here I have an array with cost, discount, time ecc.

And If I need it in JSON, I write:

$shippingcost->get("JSON"); // here I have the JSON with cost, discount, time ecc.

I wrote the Class in this way:

class ShippingCost
{
	public $state; 
	private $arrayReturned; 

	public function __construct() {  
		$this->stato="Italy"; // the default state
	}

	public function __destruct() {
	}

	public function get($format="array") {
		$this->arrayReturned=array(
			"cost" => 3.99,
			"costDiscounted" => 7.99,
			"discount" => "50%"
		);

		if (strtolower($formato)=="json")  {
			$this->arrayReturned=json_encode($this->arrayReturned);
		}
		return $this->arrayReturned;
		}
}

It works well, but I need to get the values from a MySQL db. How can pass the MySQL connection to the Class? I'm not able to do this.

 

Thanks in advance and have a great 2015.

 

Rob.

Link to comment
https://forums.phpfreaks.com/topic/293669-oop-in-php-using-mysqli-connection/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.