Hey, I’ve been programming in PHP for some time, but I’ve a serious problem with OOP, I just can’t get it (I understand some parts, but not everything - the whole thing is a bit tricky compared to easy PHP IMO)
IS PHP OOP an essential skill? Do you use OOP in every project you make?
I believe the best way to understand how it all works is by seeing simple scripts written in OOP PHP, is there a place where I can get some quality (I know it’s really easy to write crappy php) sources?
Are there books like OOP PHP Introduction for Dummies?
If you ever want to be taken seriously, you need to learn it. Once projects become more complex, it is a no brainer.
I don’t think there’s any real ‘quick’ way to learn it. Go through it as slowly as you have to, and just continually try to automate more and more by seeing patterns in your code. It may start off being totally wrong, but after enough experience and enough exposure to other code, you’ll eventually get it. Even if you have a terrible grasp, you will start to see its benefits.
The theory behind it all takes a little while to learn, but the code actually becomes much much easier to read and follow. You’ll write considerably less code, and work much more efficiently.
Having said that, when I was starting out, the PHP Application Design forum here was an absolute god-send. It took a few weeks to pick out who the brilliant posters were, and learned to follow them and learn from them. Be as active as you can there (even if it just means reading) and you’ll pick up a lot. It might not be quick, but you will learn a lot.
Also, start learning to work with frameworks or other applications. They tend to actually do useful OOP, rather than basic entity objects. Things like handling database interactions through a database object are very important to learn.
Even after 5-10 years, you will still be learning, so don’t give up after a few days, or even weeks!
It makes life a lot easier because you can write self contained, standalone, reusable code you can take from one application to another.
Hopefully this quick and simple demo will help you understand a php class (object).
I’ve created a basic object for a book with properties of author and title.
The test code creates 2 instances of the book class and plays around with the properties using the class methods.
The class code would normally be in a separate php file and “included” in your php script using the class.
<?php
class Book {
//properties
private $title;
private $author;
//------------------------------------------------------------
public function __construct($newTitle, $newAuthor) {
$this->title = $newTitle;
$this->author = $newAuthor;
}
public function setTitle($newTitle) {
$this->title = $newTitle;
}
public function setAuthor($newAuthor) {
$this->author = $newAuthor;
}
public function getTitle() {
return $this->title;
}
public function getAuthor() {
return $this->author;
}
} //end of class. The above code would normally be stored in a separate php file
//testing code
//first create 2 new books
$mybook1 = new Book('OOP is Great','Fred Smith');
$mybook2 = new Book('I like OOP','Sam Jones');
//now display the current values stored in each book
echo 'Book 1 title = '.$mybook1->getTitle().'<br />';
echo 'Book 1 author = '.$mybook1->getAuthor().'<br />';
echo 'Book 2 title = '.$mybook2->getTitle().'<br />';
echo 'Book 2 author = '.$mybook2->getAuthor().'<br /><br />';
//now change some values in each book
$mybook1->setTitle('OOP is Great - 2nd Edition');
$mybook2->setAuthor('John Smith');
//now display the current values stored in each book
echo 'Book 1 title = '.$mybook1->getTitle().'<br />';
echo 'Book 1 author = '.$mybook1->getAuthor().'<br />';
echo 'Book 2 title = '.$mybook2->getTitle().'<br />';
echo 'Book 2 author = '.$mybook2->getAuthor().'<br /><br />';
?>
IS PHP OOP an essential skill? Do you use OOP in every project you make
Define essential. Absolutely required? No. You can write code without OO ever but if you are serious about programming and see it as a career path, then yes, OOP is an absolute must unless your so geeky, your writing kernel code and device drivers.