Page 1 of 1

OOP?

Posted: Thu Aug 04, 2011 11:40 am
by JelvinJS7
This is something I've tried to look into. I couldn't find anything.

The irony here is I could program a basic PHP class and use… I think.
But my point: What is the point of object oriented programming?!

Re: OOP?

Posted: Thu Aug 04, 2011 3:33 pm
by Tino
Very basically, OOP is a way to structure and easily maintain your code.

Re: OOP?

Posted: Thu Aug 04, 2011 3:36 pm
by JelvinJS7
I don't get how that works though. If you Take out the class keyword/brackets, and remove all the "public", "private", "protected"s, etc, then you get the same thing, don't you

Re: OOP?

Posted: Thu Aug 04, 2011 4:44 pm
by jacek
JelvinJS7 wrote:I don't get how that works though. If you Take out the class keyword/brackets, and remove all the "public", "private", "protected"s, etc, then you get the same thing, don't you
Basically, yes. There is nothing that you can do with classes that you can not also do with simple functions. It's more of an organisation thing than anything else, for example, everything to do with working with a user can be in a class called "user".

Re: OOP?

Posted: Thu Aug 04, 2011 6:15 pm
by JelvinJS7
Well, would you recommend using a class on the same page as the file it's declared on, or In a back-end file, like for functions?

Re: OOP?

Posted: Thu Aug 04, 2011 8:45 pm
by Tino
Back end, definitely.

Re: OOP?

Posted: Fri Aug 05, 2011 3:15 am
by JelvinJS7
I think I'm sort of getting it… a few things:
1) How do I simply echo something with a class? Would I have to do something like this?
<?php
class Class{
public $string = "This is a string In a class that I will echo out later. ";
public $string.= "I'm quite redundant...";

public function functio_n(){
Echo "anger from not being allowed to have a function named \"function\"!";
//insert a bunch of things to be done that aren't relevant at all
}

}
?>
<?php
echo Class::$string;
?>
2) Can someone make a kind of in-depth example of how to use it, in a way that would be used for a real project (and not a way that a teacher would use)?

Re: OOP?

Posted: Fri Aug 05, 2011 8:39 am
by Kamal
This is a good tutorial http://marketplace.tutsplus.com/item/-o ... -14/122845
I purchased the last chapters (the MySQL DB class)

Re: OOP?

Posted: Sat Aug 06, 2011 10:05 am
by libeco
jacek wrote:Basically, yes. There is nothing that you can do with classes that you can not also do with simple functions. It's more of an organisation thing than anything else, for example, everything to do with working with a user can be in a class called "user".
I've been thinking about this and whether I agree. I agree that OOP is mainly an organisational thing. However, what about inheritance, you cannot do it with regular procedural code. Sure, that is used mostly for organisation too, but I still don't know whether I completely agree about your statement.

@ JelvinJS7: you might want to dig a little deeper into the basics of OOP. You're setting an object property (in OOP variables are called properties) and a public method (in OOP functions are called methods) which echoes a string. However you're echo'ing that method again, while calling it as a static. If you didn't understand what I just said: it only confirms my suspicion that you should first look into the basics (what is OOP, what is public, private, protected, static etc.). ;)

Re: OOP?

Posted: Sun Aug 07, 2011 10:25 am
by jacek
libeco wrote:I agree that OOP is mainly an organisational thing. However, what about inheritance, you cannot do it with regular procedural code.
Well inheritance has no place in procedural style code since everythign is in global scope. So you can just call any functions that exist in what ever function you are working on.

what I really mean thought, was there is no product you can make with OOP that you can;t with the old way ;) So you can make a forum using loads of functions but you can also do it using a few classes of methods, and both ways are perfectly fine as long as you keep it neat :)

Re: OOP?

Posted: Sun Aug 07, 2011 11:15 am
by libeco
Well, with that I completely agree!

Re: OOP?

Posted: Sun Aug 07, 2011 11:46 am
by JelvinJS7
libeco wrote: JelvinJS7: you might want to dig a little deeper into the basics of OOP. You're setting an object property (in OOP variables are called properties) and a public method (in OOP functions are called methods) which echoes a string. However you're echo'ing that method again, while calling it as a static. If you didn't understand what I just said: it only confirms my suspicion that you should first look into the basics (what is OOP, what is public, private, protected, static etc.).
Well I Have been trying to look Into it. I understand the difference private, public, and protected. I can't really remember what static does :blush: and I can declare a class, and then use it. Hypothetically speaking, I can extend a class (haven't tried it yet… but I know what to do ;) ).
My example was me asking. B/c in Jacek's videos, he did this:
class Example {
public function test_public(){
echo "public";
}
//...
}
//…

Example::test_public()
And I'm asking if I have to declare a function just to echo something, or (In this case), make a variable in the class, then echo it outside the class?

@Jacek: in one of your project tutorial series...things, if possible, can you also implement Classes?

Re: OOP?

Posted: Sun Aug 07, 2011 12:14 pm
by Dominion
The problem with OOP in php is next to some languages the Object orientated version is strange. The idea is to load only what you need, and yet by default php has a large library of functions to load for you...
I would suggest you look into the basics of an OOP based programming languages (e.g. Python) as once you have the basic idea of how they work it's rather easy to understand the concept, and implement within PHP. Now what do I mean by basics? Look into using Python's math functions. All you really need. Once you understand that concept, watch the "classes" tutorial Jack has made, and you should have a solid ground to start on.

Re: OOP?

Posted: Sun Aug 07, 2011 1:34 pm
by jacek
JelvinJS7 wrote:And I'm asking if I have to declare a function just to echo something, or (In this case), make a variable in the class, then echo it outside the class?
Well, I would say you should not be doing either really. If you defined the output inside the logic part of your code it all gets confusing. It depends on the situation, but I would normally have the method (what functions are called in classes) return the information only (like an array or usernames) and then output that how you want to on the actual page.
JelvinJS7 wrote:I can't really remember what static does :blush:
I don't really know the formal explanation, but a static method in a class is the same as a normal function not in a class. so you don't need an instance of the object to call it.
JelvinJS7 wrote:@Jacek: in one of your project tutorial series...things, if possible, can you also implement Classes?
I will try, but I have been trying to think of something that would work well as an example for some time. Also it just makes it so much harder to explain things because of all the awkward terminology.

Re: OOP?

Posted: Sun Aug 07, 2011 10:02 pm
by Tino
I agree it will be quite difficult. If people don't understand what they're using, implementing it makes no sense. So you would first need to make videos covering basic implementation and terminology of OOP with PHP, and then you would be able to make a series in which you implement OOP.

Otherwise I think people will get lost very easily.

Re: OOP?

Posted: Sun Aug 07, 2011 10:43 pm
by jacek
Maybe it would be a good idea to do a oop version and a normal version of the same thing ?

Re: OOP?

Posted: Tue Aug 09, 2011 12:07 pm
by xpt00x
jacek wrote:Maybe it would be a good idea to do a oop version and a normal version of the same thing ?

That's indeed a good idea. But i think it can also be a bit redundant.

Re: OOP?

Posted: Mon Oct 31, 2011 4:09 am
by JelvinJS7
Sorry to bring this up again, but I'd just been doing some research via videos, and I'm finally starting to get it! I wanted to share that.
So this quest had still been going on, and when [insert user here] posted a question about the developer API, I saw that classes were used, so I decided to watch. Then today I watched a couple of phpacademy's videos for thenewboston (alex's one video on his channel was really not helpful!). I reached an epiphany of understand! :D so this is what I think would be helpful:
So one reason oop would be helpful would be like, for example, would be like how in Facebook there's an option to see "mutual friendship" between you and a friend. So somewhere there's a user class that fetches all user info (name, the many likes, friends, etc) by using a construct function that's fed some ID. Then on the "mutual" page, it's something like
$loggedin_user = new User($_SESSION['id']);
$users_friend = new User($_GET[{other user's id}]);
Then compares the two variables.
I also get the purpose of protected and private methods/properties—to use them within another function in the class that will be used publicly, but not otherwise. (still don't get static, but there's time).

Just thought I'd share. Am I on the right track

Re: OOP?

Posted: Mon Oct 31, 2011 4:27 pm
by jacek
JelvinJS7 wrote:(still don't get static, but there's time).
Static methods in classes are basically the same as functions.

Re: OOP?

Posted: Mon Oct 31, 2011 8:06 pm
by JelvinJS7
jacek wrote:
JelvinJS7 wrote:(still don't get static, but there's time).
Static methods in classes are basically the same as functions.
So it has no specific relevance to the class in really anyway other than being in it?

Re: OOP?

Posted: Mon Oct 31, 2011 9:45 pm
by jacek
Well you can still make them private and use sell:: to refer to other methods in that class.

But they work in the same way when you call them.