I’m a rank newbie at programming. PHP is my first language and I started learning it about a year ago. I’ve made some strides, but am still pretty much dependent on my books to get by.
My question is how skilled to I need to be at procedural PHP before starting to learn PHP OOP? Is it possible to learn both at once or should a person really nail down the procedural method first?
I would suggest learning the basics of php first…ie…variable scope, functions, IF blocks etc etc.
Once you have a good understanding of the basics of procedural programming, then it is really only a small hop to learning OOP (classes/objects)
A php class, just like a class in just about any other language, is simply a file containing variables (object properties) and methods (functions to manipulate those properties). Once you understand how functions in procedural language work, then writing class methods is prety much the same. Creating class instances and calling class methods is straight forward.
In case you are not sure how classes/objects work, this simple “book” class demo shows how to create 2 instances of the book class and play around with the properties using the class methods.
<?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 />';
?>
What I did was to learn the basics of PHP first and do this until I felt comfortable to move on to OOP. This took me a few years but it’s well been worth it and the great thing is, I learning PHP & OOP at my own pace.
Procedural and Object Oriented languages are very different in the way you use them and knowing one will if anything slow you down in learning the other. Almost everything you learn about writing procedural code has to be discarded in order to use object oriented properly.
PHP is my second language my first is Object Pascal (Delphi).
I find some simple things that are common with objecty languages to be a bit irritating with PHP. For example, if I have a list of strings in Pascal I can do:
list.count to find how many strings are in the list. In PHP there is a count() function that I must pass the list (array) into.
I was using arrays as stacks recently, it seems wrong these days to use array_push($myStack, Item) rather than $myStack.push(Item).
I would suggest you can gradually learn OO php by creating classes that use methods to perform common tasks, rather than functions.
If you don’t mind be asking what is the extent of your experience using PHP in a business environment. Knowing the fundamentals of PHP is one thing, having experience with it in a practical non-academic setting is another. If you believe you have the basics down learning OOP is a good next step. However, if your still having issues with arrays or basic concepts of programming its probably best to hold off a little until you aren’t having those issues any more. OOP is essentially just another layer. So having a good understanding of conditionals, loops, variables, etc is essential to grasping OO methodology and practical application to real-word scenarios.
In my case it did work out to be a good half-way house to start increasing the use of functions.
Then move those functions into their own files and include them.
Then keep families of functions in the same files.
If you don’t understand how functions work, if you come to try and learn OOP then you come a right cropper.
I certainly did, I ended up with a horrendous learning curve.
The same goes for arrays and basic language constructs, do some studying as if you were practicing to take your ZCE (PHP Exam).
If you are good at all the practice questions, then maybe its a good time to start learning OOP, but not before.
I did it that way round, and whilst I found the whole thing mentally stimulating, I just did not have the basic skills to make the most of my OOP learning.