TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   PHP OOP - Am I the only one? (http://www.talkphp.com/general/3795-php-oop-am-i-only-one.html)

Mithadriel 12-23-2008 02:29 PM

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!

Village Idiot 12-23-2008 03:16 PM

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.

Orc 12-23-2008 05:15 PM

Which part do you need help with?

masfenix 12-23-2008 06:58 PM

Try working with java or c#.net. You'll be forced to use OOP.

geo353 12-23-2008 08:08 PM

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.

Tanax 12-23-2008 09:11 PM

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).

Measter 12-24-2008 12:59 AM

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.

CoryMathews 12-24-2008 04:17 AM

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.

Crazymik3 12-24-2008 01:59 PM

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.

Village Idiot 12-24-2008 03:49 PM

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.

geo353 12-24-2008 04:29 PM

Quote:

Originally Posted by Village Idiot (Post 20693)
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.

maZtah 01-07-2009 08:49 AM

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.

sarmenhb 01-08-2009 11:56 PM

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!.

CoryMathews 01-09-2009 12:27 AM

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.

allworknoplay 03-17-2009 02:10 AM

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..

triumvirat 03-17-2009 08:01 AM

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)

delayedinsanity 03-17-2009 08:40 AM

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.

triumvirat 03-17-2009 09:02 AM

hello world in patterns OOP-style: http://www.phppatterns.com/docs/desi...ld_in_patterns

allworknoplay 03-17-2009 03:47 PM

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 03-17-2009 03:49 PM

Quote:

Originally Posted by delayedinsanity (Post 22286)
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...


All times are GMT. The time now is 11:54 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0