Newb with OOP, Question concerning it.

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Newb with OOP, Question concerning it.

Post by akimm »

Hello all, I have been teaching myself php for a few months here, and in my quest to become proficient I have come across the question many times, should I learn the OOP of PHP. If anyone can give me some idea, pros and cons of it, what their preference is, et cetera.

Secondly, if php OOP is not worth learning what languages would be a suggested place to learn, because, I really like the way OOP works, for the little I know. I just worry, for my redundant tasks, that OOP is sort-of overdoing it.

Thanks all!

Sean.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

OOP is not restricted to PHP :)

You will get a solid "Yes" from just about everyone on this board - learn OOP practices, and use them.
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

The general OOP concepts are common to a large number of languages including Java and C++.

That would be a definite YES! :)
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

I'm 100% supportive of anybody learning OOP, but akimm, I'd suggest you master the basics first. Get used to writing logical code before you try to dive any deeper.
jajumbo
Forum Newbie
Posts: 6
Joined: Wed Jul 26, 2006 4:32 pm

Post by jajumbo »

OOP is not as difficult as it sound, but it is not as easy as some people may lead you to think.

I would say, Yes to learn OOP, No to restrict it to PHP only.

A good starting point to learn OOP is simply Java.

Cpp, u don't want to go there, unless you want to start playing with ptr->ptr->ptr->ptr->ptr->

php, you can still do something like that, but it is not as difficult as Cpp or Java.

Also, if you want to really use and learn OOP,

I suggest php5.

because php4 does not support public/private tags..
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

yea I didn't understand or like OOP at all until PHP5 when PHP finally got it (for the most part) right. If OOP was, in other languages, how it is in PHP4, I doubt it would be so popular.
jajumbo
Forum Newbie
Posts: 6
Joined: Wed Jul 26, 2006 4:32 pm

Post by jajumbo »

The Ninja Space Goat wrote:yea I didn't understand or like OOP at all until PHP5 when PHP finally got it (for the most part) right. If OOP was, in other languages, how it is in PHP4, I doubt it would be so popular.
Oh there are many out there like that.

OOP has been around for a while... well first it wasn't fully OOP, but nowadays, atleast the very least you should be able to create private and public variables.

php 4's style of OOP, to me, is making life easy for php developers.

For example, if many coding languages, object's default internal variables are all private or protected.
of coz, then there are those that are automatically public.

php4 follows the later one but does not allow you to turn it into a private mode. Also it seems all functions written in PHP are public.

While in most OOP languages that I know, functions can also be public/protected/private... don't forget, virtual :)

PHP seems to be in between oop and Imperative.

that's why it was so easy to pick up for me.
To this date, I've only been using php for 2 months. but I have been coding in Java, C, Cpp, VB for awhile. :)
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

php5 saw the inclusion of public/private/protected keywords for use in class properties and methods.

Code: Select all

class Foo
{

    private $bar;

    public function fooBar()
    {}
}
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Newb with OOP, Question concerning it.

Post by Christopher »

akimm wrote:I just worry, for my redundant tasks, that OOP is sort-of overdoing it.
I think for "redundant tasks" OOP should be a time saver for you. It is in the little one-off scripts where procedural PHP works well -- essentially where the entire script is a single class.
(#10850)
hrubos
Forum Contributor
Posts: 172
Joined: Sat Oct 07, 2006 3:44 pm

Post by hrubos »

I think you should learn OPP in Java or C++
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

There are really no cons to OOP in PHP, especially with the way in which PHP5 handles OOP. I would suggest you wrap your mind around logical procedural code flow (not necessarily procedural coding, but the flow of the code) before you get too deep into OOP as you are going to want to be able to code cleanly and efficiently to really unleash the full power of OOP. That is not to say that bad code can't work in OOP, but to really use OOP in the manner that it is best suited to, you are going to want to make sure your code is clean.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

I'm personally a procedural programmer. Although, I can code php4 style classes and use them in conjunction (right word?) with other objects.. thus being a certain degree of Object Oriented.

I'd say if you're not working in a group environment, do whatever makes you more comfortable. While learning OOP, code procedurally until you're comfortable enough to go all out.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
alvinphp
Forum Contributor
Posts: 380
Joined: Wed Sep 21, 2005 11:47 am

Post by alvinphp »

I personally say you go straight to OOP. Too often it seems people go to procedural and then never get out mainly because it is working so why change. OOP will not only be beneficial for PHP, but for many other languages.
Post Reply