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