![]() |
help me understand OOP
what im trying to do is make a class that will somehow strip the hour,minute, and seconds from this function system("time") and display it when i do a call to the class. the reason im doing this is because im trying to make sense of oop and cant. for some reason i cant understand how to use oop in a page.
i need some help here, i have read chapter n chapters and saw videos online and cant seem to make sense of it. heres my code Code:
|
What exactly cant you make sense of? the actual class or how it works?
|
Hi Sarmenhb,
I've re-written your class a little bit in the hope it will make more sense. Also, I've put lots of comments along with a working example at the bottom so hopefully it will help you understand :-) PHP Code:
Alan |
thanks for the code ill print and review it.
i do have a question, so in the methods(functions) in a class can i put any normal thing that a regular function had for example if i wanted a function to do this Code:
function test($name,$age) |
Yep, anything you can do in normal functions you can do in class functions/methods.
Alan |
Quote:
so then this code would work? Code:
|
Put an echo '' around 'you are old' and it should work fine :-)
Alan |
Quote:
so then oop is very easy , its all common sense basically if you understand functions, variables then its ez so basically anything that can be done in basic php coding can be setup in oop lol, damn books make it confusing. |
A little tip for you as well when using classes:
Try not to echo anything from your class. You should always try to make your class self-contained so that you can just drop it into a project and have it work. If you echo something from your class then take that class and re-use it in another project that has (for example) a template engine then you would want to parse all text through your template engine rather than echo it. So in your class above, rather than echo the visitors name, it would be better to return it, then echo it in your main script. Just keeps everything nice, neat and tidy :-) Alan |
Quote:
i heard a click in my head the tunnel is bright |
OOP is recommended over procedural code for exactly this thing (amongst others, of course): not outputting stuff. A class is a library, which performs its inner workings, but the output should be implemented by another library and/or by the application itself. The logic class is ment to work when ever, where ever is needed, without having to worry about what content it generates.
|
Quote:
Just for those people learning from this topic, the syntax errors include:
Are you clear on what OOP is all about because no-one has really explained anything in this topic, yet. It's not quite as simple as wrapping functions up within a class! |
Quote:
|
thnx, im getting it, i need to practice with it and play around with it.
maybe when i understand it i can write an article :) |
I have made a tutorial on OOP if you want to have a read though.
Programming Tips PHP and OOP (full) |
Quote:
|
| All times are GMT. The time now is 11:02 AM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0