I need help understanding more about PHP OOP

I have a little experience in procedural PHP years ago and have not really programmed in several years. I’m reading books and tutorials to try to grasp OOP better, but some things have not clicked yet. I have a simple input submit that sends the text to jquery and then uses ajax to PHP. Once at the PHP page I try to sanitize it with the below.


/* Sanitize string text */
class SanitizeText {
	public $SanitizeText;
	
	public function __construct($SanitizeText) {
		$this->SanitizeText = filter_var($SanitizeText, FILTER_SANITIZE_STRING);
	}
}

What will be the next proper OOP style step if I need to insert it into a database? Do I create another file with another class strictly for inserting data so the code can be used again from future data inserts? If so, I am not sure how to properly extend this class to a class to insert the data.

Thanks.

I suggest using some kind of PHP framework like CodeIgniter; it will help you learn to use objects and classes better.

For me in your code, I would have a database class that is used to interact with your database, but then also another class (a model) that would be used for inserting the data into the database. You could have a class like:


class newmodel extends model {

public function insert_data($data) {
     $this->db->insert("table", array("data" => $data);
}

}

Here you can see the database class inside the model being used. You could then reuse the model class whereever you need to perform that action (adding data to your table). Sorry if this wasn’t too clear. Check out the CodeIgniter User Guide, it’s really simple: http://ellislab.com/codeigniter/user-guide/

Thanks. Ill check it out.

Hi,

It may seem like an unusual recommendation but learning the fundamentals and process of OOP design will best serve you learning it.

To this end, Behat ( behat.org ) is a tool that will let you express in words what you want to do and will write skelton classes for you. It will stop you from focusing how to code and more about what you want to do.

I would then look to learning TDD ( Test Driven Development ) using PHPunit. TDD allows you to build-up the Behat skelton classes in the most simplistic, step by step manner. It focuses your thinking and helps you not get side-tracked with all the new things regarding OOP going through your head. It also ( done right ) will allow you to have flexible and decoupled designs.

You should also invest time in learning code patterns. Martin Fowler ( martinfowler.com ), is a great writer and mind when it comes to OOP and design processes. You would benefit from reading refactoring and code books and online material of Martin.

Another author you may want to explore is Fabian Potencier ( http://fabien.potencier.org/ ). Fabian is an active author in the PHP domain and is a skilled designer. He has authored the Symphony framework and smaller projects like Pimple ( Dependency Injection Container for PHP ). You may want to study the code of Symphony and other projects Fabian has created as it will teach you a lot regarding OOP.

There are people in this forum that are very good at OOP but to best leverage their knowledge it best serves you to invest in learning fundamentals and asking questions relative to concepts and technologies that you face along this journey.

Regards,
Steve

What I have for database interaction is the following.

I use the Model View Controller ( MVC ) pattern so for every database table I have a model that extends a parent class that handles all your basic CRUD ( create, read, update, delete ) functionality. That way we can automatically build our queries by using a few simple methods. For example you can have a method to get all the data from a database.


//Instantiate the example model
$example_model = new Example_model();
//Get all the rows
$all_data= $example_model->all();

Because the table name is in the class name we can grab that in the constructor and use it to automatically form our queries.

This is a very simple example but a part of OOP programming is all about building non specific code that can be used over multiple projects. You can write a save method that automatically determines whether to insert or update based on if a ID is passed in. Then of course you would need methods to set where, limits and order by clauses but these are very simple because they would just basically be setting the model properties so the method that builds the query can get them and form the final query.

Hope I didn’t confuse you!