Part of the EllisLab Network
   
 
Start with procedural or MVC OOP?
Posted: 25 March 2007 02:33 PM   [ Ignore ]  
Summer Student
Avatar
Total Posts:  9
Joined  03-24-2007

Hey again,

I’m ready to start producing my first PHP applications and I would like your opinion on something.

I’ve been researching MVC and I’m slowly getting my head around the concept, it’s more OOP that is confusing me.

So is it worth forgetting about writing procedural code that could influence bad habbits in the future, or start from day 1 with MVC and OOP?

In a way I feel that if i start with Code Igniter, I’m cheating in a way because alot of the code is already written for me…

What did you start with?

Rob

Profile
 
 
Posted: 25 March 2007 02:48 PM   [ Ignore ]   [ # 1 ]  
Research Assistant
RankRankRank
Total Posts:  970
Joined  04-13-2006

Hi Rob.
The answer is probably “it all depends” - just what you didn’t want to hear.  wink
By the sound of it, you know some PHP but no OOP. Well, as you know, CI is basically an OOP operation, so I would definitely start moving away from procedural code and beginning to work with OOP. I would also suggest not worrying about the MVC business until you’re more comfortable working with classes.
This might not be the best place to get a basic knowledge of OOP. Don’t hesitate to use other sources of reference - for example, the PHP manual includes good explanations of classes and objects.
Working with Code Igniter isn’t really cheating. However, it would be a Good Thing to try to understand what it’s doing and how it does it rather than just take it all for granted. It’s generally well-written code which is worth the effort of understanding.
Finally, there are as many different opinions on this as there are liars in politics. Try to stick to a small number of reference sources which you feel you understand, and always ask in here when you’re really stuck.

Profile
 
 
Posted: 25 March 2007 03:45 PM   [ Ignore ]   [ # 2 ]  
Summer Student
Avatar
Total Posts:  9
Joined  03-24-2007

Thanks for the speedy response!

I think learning and understanding OOP is probably the best place to begin with then. I’ve found a few tutorials and have been reading the PHP 5 manual on OOP. I understand the basic theory - but not how to put it in practice (ie. ‘$this->’ just totally confuses me when it’s being used all over the place!), hopefully when I start coding my own OOP PHP then I’ll understand how it all works.

Profile
 
 
Posted: 27 March 2007 11:42 AM   [ Ignore ]   [ # 3 ]  
Grad Student
Avatar
Rank
Total Posts:  59
Joined  11-13-2006

Learning good OOP practices is a great thing to do. I’m still getting there, so my code is semi-OOP (or semi-procedural depending on how you look at it).

One thing to keep in mind, though, is if you are using PHP5 features, that the production environment supports PHP5. One of the reasons I chose CI is that it still fully supports PHP4. While I have upgraded and my development servers are PHP5, many hosting environments are still PHP4, which limits much of the OOP features you can use.

Profile
 
 
Posted: 27 March 2007 01:26 PM   [ Ignore ]   [ # 4 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1740
Joined  06-23-2006

To put OOP in simple terms, you want to think about everything as objects, in a parent-child relationship. Some objects can inherit qualities from parent classes by using the extends keyword.

$this-> simply means “my attribute, or if I don’t have it, look at my inheritance for that attribute.”

class Person {
    
var $name        = '';
}
class Teacher extends Person {
    
var $id          = 0;
    var
$room_number = 0;
}
class Student extends Person {
    
var $id          = 0;
    var
$teacher_id  = 0;
}

In the above example, Teacher and Student are both subclasses of a general Person class. All Persons have a name. Teachers and Students add attributes specific to them, and inherit the name attribute from Person. You can see that Teacher and Student have their own $id attribute, but a general Person doesn’t.

Once you go OOP, you never go back.

 Signature 

Mac OS X 10.4.10, Apache 1.3.3, PHP 5.2.3, CodeIgniter 1.5.x., baby!

Profile
 
 
Posted: 27 March 2007 01:45 PM   [ Ignore ]   [ # 5 ]  
Summer Student
Avatar
Total Posts:  9
Joined  03-24-2007

Coolfactor, thanks for the run through - I’m slowly getting my head around OOP. I think I need to stop trying to understand it fully and start programming in it, hopefully then it will start making more sense.

[n00b question] Could you explain why you used “var $id = 0;” instead of “$id = 0;”? [/n00b question]

Profile
 
 
Posted: 27 March 2007 01:59 PM   [ Ignore ]   [ # 6 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1740
Joined  06-23-2006

[n00b question] Could you explain why you used β€œvar $id = 0;” instead of β€œ$id = 0;”? [/n00b question]

To define variables (properties) in classes, you must use the var keyword.

 Signature 

Mac OS X 10.4.10, Apache 1.3.3, PHP 5.2.3, CodeIgniter 1.5.x., baby!

Profile
 
 
Posted: 27 March 2007 04:25 PM   [ Ignore ]   [ # 7 ]  
Summer Student
Avatar
Total Posts:  9
Joined  03-24-2007

Aha, makes sense now. I’ve been coding some basic OOP examples all evening and it’s all making sense. I have a question though…

Is there a way to output the value of a variable of an object, without making a new function in the class?

something like….

echo $object->var_name

..well I went ahead and tested my assumption and it turns out you can do that, I’m actually learning on my own by testing stuff out.. OOP is great :D

Now to try something harder like returning arrays and using databases with OOP… *hides*!

Profile
 
 
Posted: 27 March 2007 04:34 PM   [ Ignore ]   [ # 8 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1740
Joined  06-23-2006
rawkes - 27 March 2007 04:25 PM

Is there a way to output the value of a variable of an object, without making a new function in the class?

Yes, that definitely works, but I would recommend against it on a regular basis. One aspect of OOP is the concept of encapsulation, which essentially means:  the internal structure/design of a given class should be independent of its interface. The designer of a class should be able to change the internals without affecting code that uses that class. Using “accessor” methods is the best way to go about this.

No hard rules about it, just guidelines.

 Signature 

Mac OS X 10.4.10, Apache 1.3.3, PHP 5.2.3, CodeIgniter 1.5.x., baby!

Profile
 
 
Posted: 27 March 2007 04:42 PM   [ Ignore ]   [ # 9 ]  
Summer Student
Avatar
Total Posts:  9
Joined  03-24-2007

Could you give me an example of how you would do it?

Profile
 
 
Posted: 27 March 2007 04:53 PM   [ Ignore ]   [ # 10 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1740
Joined  06-23-2006
rawkes - 27 March 2007 04:42 PM

Could you give me an example of how you would do it?

class Dog {
   
var $color  = '';

   function
set_color($color) {
       $this
->color = $color;
   
}
}

You may choose to change the internal name of the “color” property to something else, and update the method, but the interface would remain consistent…. you’d still call $this->dog->set_color(‘red’); regardless of the internal variable name.

Again, just guidelines… depending on the complexity of your classes, or who is going to be implementing them, you may want to use methods for both “setting” and “getting” variables. In my own code, I just access properties directly because I know exactly how they’re going to be implemented.

There’s no hand-and-fast rules, just recommended ways. My recommendation:  start by using methods, and then as you get more comfortable, you can begin to access properties directly. Methods are definitely safer.

You can even create generic methods:

class Dog {
   
var $color = '';

   function
set($k, $v) {
       
if (isset($this->$k)) {
           $this
->$k = $v;
       
}
   }

    
function get($k) {
        
if (isset($this->$k)) {
            
return $this->$k;
        
}
    }
}

In the above code, I created generic get() and set() methods that both checked if a variable existed first. This is a good way to ensure that code changes won’t break your application and generate errors.

 Signature 

Mac OS X 10.4.10, Apache 1.3.3, PHP 5.2.3, CodeIgniter 1.5.x., baby!

Profile
 
 
Posted: 27 March 2007 05:41 PM   [ Ignore ]   [ # 11 ]  
Summer Student
Avatar
Total Posts:  9
Joined  03-24-2007

coolfactor, I can’t thank you enough. You obviously know your stuff and you’ve already taught me alot in the space of one evening!

I totally agree with what your saying though, it makes sense now. I suppose coding OOP is one thing, but getting my head around the design practice and concept of OOP is another. I’m sure it will come with time and practice.

If you have any resources or book recommendations for the design practice and theory of OOP I’d love to know about them.

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 719, on June 06, 2008 10:16 AM
Total Registered Members: 66415 Total Logged-in Users: 36
Total Topics: 84759 Total Anonymous Users: 2
Total Replies: 454882 Total Guests: 264
Total Posts: 539641    
Members ( View Memberlist )