TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 12-23-2008, 02:29 PM   #1 (permalink)
The Wanderer
 
Join Date: Nov 2008
Posts: 5
Thanks: 2
Mithadriel is on a distinguished road
Default PHP OOP - Am I the only one?

Who finds it terribly perplexing :P

I've read god knows how many online tutorials and explanations, and whilst the theory makes sense, when I sit down to do something as simple as connect to a database I manage to get myself in a muddle.

Procedural stuff is simpler for me to understand but I just can't get my head around the whole OOP thing.

Back to practicing ... I was just curious if I was being particularly stupid!
Mithadriel is offline  
Reply With Quote
Old 12-23-2008, 03:16 PM   #2 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

It is common for people not to understand OOP with PHP. I can't really relate though because I understood OOP before I ever started PHP.
__________________

Village Idiot is offline  
Reply With Quote
Old 12-23-2008, 05:15 PM   #3 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Which part do you need help with?
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 12-23-2008, 06:58 PM   #4 (permalink)
The Contributor
 
Join Date: Mar 2008
Posts: 31
Thanks: 1
masfenix is on a distinguished road
Default

Try working with java or c#.net. You'll be forced to use OOP.
masfenix is offline  
Reply With Quote
Old 12-23-2008, 08:08 PM   #5 (permalink)
The Wanderer
 
geo353's Avatar
 
Join Date: Dec 2008
Location: Wiltshire, UK
Posts: 13
Thanks: 0
geo353 is on a distinguished road
Default

I like to see OOP as a way of thinking, even in java you can write procedural code. It takes practice but it will click eventually.



I find designing is as important as building the actual app, try drawing a few relational scribbles of the objects and work out in you’re mind how you think it should work. This should help you grasp the concept of objects. UML is also a great way to learn good OOP design.
geo353 is offline  
Reply With Quote
Old 12-23-2008, 09:11 PM   #6 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

I don't know. I found it extremely simple, to use classes. Took me like 1-2 weeks to get the hang of it. Ofcourse you need to practice to do more complex things.

I watched video tutorial from jonathan sampson, try search for him on google! Extremely good tutorials where he talks and explains classes(again; only the basics, but once you understand that, you can understand the more advanced OOP tutorials that are written, and not filmed).
__________________
Tanax is offline  
Reply With Quote
Old 12-24-2008, 12:59 AM   #7 (permalink)
The Wanderer
 
Join Date: Nov 2008
Location: Plymouth, UK
Posts: 9
Thanks: 0
Measter is on a distinguished road
Default

I also had a lot of trouble understand OOP, even after reading several tutorials. I only really started to understand how classes worked after I wrote one.
I suggest you get stuck in with something simple, perhaps something that simply adds a couple strings together then outputs it, just to get a feel for it.
Measter is offline  
Reply With Quote
Old 12-24-2008, 04:17 AM   #8 (permalink)
The Addict
 
CoryMathews's Avatar
 
Join Date: Nov 2007
Location: USA
Posts: 256
Thanks: 7
CoryMathews is on a distinguished road
Default

Inheritance is what finally made it all click for me. Write a base class such as a user class then a class such as admin and a class for a regular user (just an example) then make those two classes inherit functions from the main user class. Once this concept finally clicked it all just became easy.
CoryMathews is offline  
Reply With Quote
Old 12-24-2008, 01:59 PM   #9 (permalink)
The Wanderer
 
Join Date: Jul 2008
Posts: 5
Thanks: 0
Crazymik3 is on a distinguished road
Default

Learn Java, and I garuntee that you will learn OOP by the time you're used to Java.
Since I came to PHP after I did Java, I was basically used to OOP in PHP. You'll get used to it soon, don't worry!

Here's a simple task you can do with OOP, and address book.
Crazymik3 is offline  
Reply With Quote
Old 12-24-2008, 03:49 PM   #10 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

The problem I've always found with most OOP tutorials if that the examples are practically useless. OOP is really of no advantage whatsoever for small scripts, which are all that can be covered in small tutorials. OOP begins to make sense when you begin designing large scripts with big portions of code that must be reused.
__________________

Village Idiot is offline  
Reply With Quote
Old 12-24-2008, 04:29 PM   #11 (permalink)
The Wanderer
 
geo353's Avatar
 
Join Date: Dec 2008
Location: Wiltshire, UK
Posts: 13
Thanks: 0
geo353 is on a distinguished road
Default

Quote:
Originally Posted by Village Idiot View Post
The problem I've always found with most OOP tutorials if that the examples are practically useless. OOP is really of no advantage whatsoever for small scripts, which are all that can be covered in small tutorials. OOP begins to make sense when you begin designing large scripts with big portions of code that must be reused.
Very good point, not only that though, most tutorials teach you how to build a class and put some methods in it but never really teach you the real power of OOP which is its design. Its very easy to create classes filled with procedural code but to actually create useful and reusable objects you need to first see the bigger picture, which i believe will be easier when designing a large system where code reuse is a must.
geo353 is offline  
Reply With Quote
Old 01-07-2009, 08:49 AM   #12 (permalink)
The Acquainted
 
Join Date: Oct 2007
Posts: 170
Thanks: 18
maZtah is an unknown quantity at this point
Default

I am also having trouble writing OOP. I fully understand all the rules and I know what everything means/does.

But when I am wanting to write some useful application, I get stuck.

Maybe it's a good idea on a few people giving us some (basic) exercises we have to write OOP. Then discuss it later. The level should go from basic to advance within a weeks?

For example; first we have to write a basic Database class with a few (by you defined methods) in it. Later on we will cover inheritance etc.

Please reply on this post!


Edit: Also useful exercises are to write a class which gets like all products from a database and lists them on a page. The thing I struggle with is that I don't know whether to make an object for every product or not.
maZtah is offline  
Reply With Quote
Old 01-08-2009, 11:56 PM   #13 (permalink)
The Addict
 
sarmenhb's Avatar
 
Join Date: Jan 2008
Location: los angeles
Posts: 309
Thanks: 44
sarmenhb is on a distinguished road
Default

you dont really have to use oop the whole purpose of it is to reuse code on multiple pages rather than having to retype a bunch of things.

OOP is just a bunch of functions places all together and work together.

here is an example
i kind of forgot how its written but ill try and explain

Code:
Class phpConnect {

var host 
var login;
var password;
var dbname;

function connect_info{) {
//$this->host is the same as writing phpConnect->host
//its much shorter to write $this rather than the whole class name

$this->host = "localhost";
$this->login = "username";
$this->password = "password";
$this->dbname = "somedb";
}

function connect() {

return mysql_connect($this->host,$this->username,$this->password);
return mysql_select_db($this->dbname);
}

$con = new phpConnect();
$con->connect;
maybe i wrote it wrong but someone correct me if i did but basically now you cna just include this page onto every page that you want the connection to occure then again you could of made it shorter by just doing

Code:
mysql_connect("localhost","root","password");
mysql_select_db("dbname");
the purpose of the class is to instantiate an existing bunch of functions to reuse them in seperate ways instead of using it in one way. my best suggestion would be to checkout as much as classes as possible , give it 1 month, 5 months , 1 year, untill you see its purpose. you dont need to rush into it because you can do the same thing in fewer lines. you will see its purpose once it clicks into you, once it does it will make ur life easier.

as for me, this whole time that i've been doing php i havent used classes yet but i should because i need to create a few tools with classes so all i need to do is type one line of code the next time and all my hours spent on making something will be cut down to 1 minute!.
__________________
no signature set
sarmenhb is offline  
Reply With Quote
Old 01-09-2009, 12:27 AM   #14 (permalink)
The Addict
 
CoryMathews's Avatar
 
Join Date: Nov 2007
Location: USA
Posts: 256
Thanks: 7
CoryMathews is on a distinguished road
Default

In the previous example it is more work but say you have to connect to 10 or even 50 different databases. You would modify the class just a little to be something where you just declare an instance with

$con1 = new phpConnect("dbname1");

that code would be much shorter and easier to manage then 50 sepereate procedural declarations.

One benefit of OOP is that it also allows you to more easily reuse code. So on your next project you just take the db class from your last project and you already have that chunk of code written.
CoryMathews is offline  
Reply With Quote
Old 03-17-2009, 02:10 AM   #15 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

I am trying to learn OOP as well. Village Idiot has a great point.

I've read about 4 books on OOP recently, well over 1000 pages total of OOP and it just takes time to learn. The examples are always small so they are useless. It's only when you are part of a big project where you begin to see the scope and power of OO design...

I wish I started off with another language like Java so that the transition to OOP would be a little easier..
allworknoplay is offline  
Reply With Quote
Old 03-17-2009, 08:01 AM   #16 (permalink)
The Wanderer
 
triumvirat's Avatar
 
Join Date: Dec 2008
Location: Russia, Moscow
Posts: 14
Thanks: 0
triumvirat is on a distinguished road
Default

You are need reading true books, the classical manuals: Martin Fowler, Gradi Buch and other. It's fundamental authors.

I think, the main problem in OOP-style - it's intersection the relation model data and object model. (See ORM http://en.wikipedia.org/wiki/Object-relational_mapping)
triumvirat is offline  
Reply With Quote
Old 03-17-2009, 08:40 AM   #17 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

Quote:
The examples are always small so they are useless.
I kind of disagree that short examples are useless. They are if you already understand what you're doing, but for somebody just starting out, you need it. That's why the most famous first example in the world for almost every language is something along the lines of;

PHP Code:
echo 'Hello, World!'
Simple, short, and gets your feet wet. Now we look at OOP;

PHP Code:
class hello_world
{
    public 
$hello_string 'Hello, World!';

    public function 
write_string()
    {
        echo 
$this->hello_string;
    }

}

$my_first_time = new hello_world;
$my_first_time->write_string(); 
Short, and doesn't accomplish a thing we can't do procedurally twice as fast and with 10% of the code. So is it useless? I don't believe so. It gets your feet wet. While you may not understand why it's working the way it is, you've just written your first class, and you're starting to integrate the knowledge of how it's all put together. The problem with some of those tutorials probably lies within the authors ability to convey the necessity of OOP and when and where it can be used most effectively.
delayedinsanity is offline  
Reply With Quote
Old 03-17-2009, 09:02 AM   #18 (permalink)
The Wanderer
 
triumvirat's Avatar
 
Join Date: Dec 2008
Location: Russia, Moscow
Posts: 14
Thanks: 0
triumvirat is on a distinguished road
Default

hello world in patterns OOP-style: http://www.phppatterns.com/docs/desi...ld_in_patterns
triumvirat is offline  
Reply With Quote
Old 03-17-2009, 03:47 PM   #19 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Correct. The fundamentals are very important. I read this one book that was on fundamentals and concepts of OO design so it had nothing to do with PHP.

I just need to get more experience with it on a daily basis...sometimes you have to put the books down and just get to it!
allworknoplay is offline  
Reply With Quote
Old 03-17-2009, 03:49 PM   #20 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by delayedinsanity View Post
I kind of disagree that short examples are useless. They are if you already understand what you're doing, but for somebody just starting out, you need it. That's why the most famous first example in the world for almost every language is something along the lines of;

PHP Code:
echo 'Hello, World!'
Simple, short, and gets your feet wet. Now we look at OOP;

PHP Code:
class hello_world
{
    public 
$hello_string 'Hello, World!';

    public function 
write_string()
    {
        echo 
$this->hello_string;
    }

}

$my_first_time = new hello_world;
$my_first_time->write_string(); 
Short, and doesn't accomplish a thing we can't do procedurally twice as fast and with 10% of the code. So is it useless? I don't believe so. It gets your feet wet. While you may not understand why it's working the way it is, you've just written your first class, and you're starting to integrate the knowledge of how it's all put together. The problem with some of those tutorials probably lies within the authors ability to convey the necessity of OOP and when and where it can be used most effectively.

Right. I guess I worded it wrong. Short codes like this help you see the STRUCTURE of OO so you can begin to understand how OO functions.

I guess what I was trying to say is that it's hard to see the power and scope of OO with short simple scripts...

Like you said, all that work just to print Hello World can be done in .0002 seconds with regular procedural code so anyone learning OO would have to understand that you wouldn't use OO for the sake of printing JUST Hello World...
allworknoplay is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
10 PHP Myths Dispelled Wildhoney General 9 06-15-2009 06:55 AM
PHP Books, which and why? Jim General 0 06-27-2008 07:18 PM
PHP Compressor Kalle Script Giveaway 8 05-28-2008 12:14 AM
what are all the subjects in php? sarmenhb General 7 01-21-2008 05:41 PM
Uploading Files with PHP daz Absolute Beginners 3 09-30-2007 06:23 PM


All times are GMT. The time now is 09:27 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design