Razor42 Posted September 7, 2012 Share Posted September 7, 2012 Hey there, As many of you know I have been learning and learning and learning and my skills are slowley increasing and I am becoming better and better, now one thing I discovered by speaking to a few people and by using google is Object-Oriented PHP or OOP. OOP is a style of coding in which you group your actions into classes. Now I have learned abit of OOP and I am making OKAY progress with it. I can show a small example of a little time tool I created using OOP. Time.php <?php include ('class_time.php'); //includes the class file $otime = new time; $stime = $otime->GenerateCurrentTime(); print 'The Time is:' . $stime; ?> class_time.php <?php class time{ //creates a class with a fucntion to generate time. var $stime; function _construct($sPresetDate = false){ if($sPresetDate){ $this->stime = $sPresetDate; } else { $this->GenerateCurrentTime(); } } //function to generate time. function generatecurrenttime(){ //sets date & time. Days. Months. Years. 24 hour clock. $this->stime = gmdate("d-m-Y H:i:s"); } //function to show future dates by adding dates. function ShowFutureDate($iAddDays=0){ //Gets the date, Adds days then gets the date for them added days. $this->stime = gmdate("d-m-Y H:i:s", strtotime("+" .$iAddDays . "days")); } } ?> Now after that what I want to know is can learning OOP and the use of classes actually help me? I have read on many sites that it is a effective and compact way of coding, also through personal use it may also be easy to use as you can just reffer to these classes when needed. Quote Link to comment Share on other sites More sharing options...
Danny696 Posted September 7, 2012 Share Posted September 7, 2012 OOP is actually Object-Oriented PROGRAMMING, not PHP. OOP can be usefull, it depends on what your doing, and what's your preference. Also, in time.php, line 3 should be; $otime = new time(); As you have a construct function, use it. Quote Link to comment Share on other sites More sharing options...
Razor42 Posted September 7, 2012 Author Share Posted September 7, 2012 OOP is actually Object-Oriented PROGRAMMING, not PHP. OOP can be usefull, it depends on what your doing, and what's your preference. Also, in time.php, line 3 should be; $otime = new time(); As you have a construct function, use it. Some slight mistakes and just missing a small thing out. Quote Link to comment Share on other sites More sharing options...
Djkanna Posted September 8, 2012 Share Posted September 8, 2012 "Is Object-Oriented PHP (OOP) A Effective Code" - I'm not entirely sure what you mean by this. "Will It Improve My Coding" - Again not sure what you mean by this either. In a sense of will it further better your abilities: Sure learning new things is a good thing, no different in this case. In a sense of will it improve my coding: I don't think so, if your code is bad (bad choice of word, but I think you get the point) beforehand chances are you'll repeat the same no matter what paradigm you use (Procedural, Object-Oriented). On the subject of classes; I find them to be extremely useful/powerful in terms of reusable code (notice most 3rd party libs are in the form of classes), those things you generally find yourself writing every time you start a new project. If you're writing a(-a) class(es) that is(are) used once throughout your project, you're probably going overboard. Good luck with your learning. Quote Link to comment Share on other sites More sharing options...
Razor42 Posted September 8, 2012 Author Share Posted September 8, 2012 I wouldn't say my coding is bad, I'd say its "okay". Its coming along obviously I have a long way to go but I'm learning alot and improving alot :). I the main thing I wanted to know is do many people use OOP and do they find it useful? And also I read on a few places that it can make my coding more compact, effective code. Also that it can save alot of time? Quote Link to comment Share on other sites More sharing options...
a_bertrand Posted September 8, 2012 Share Posted September 8, 2012 It depends pretty much on what you do, and the language / api you work with. In PHP honestly, you can do most (even re-usable code) without yet using much OO. Some times OO is also nearly a requirement for PHP but overall the implementation of the OO in PHP is not what I like most. In C# on the other side, I code only in OO, not only because the framework is all OO, but because it makes me the life indeed much simpler and allows to make things in a smarter way. Yet think that what you can do in OO can be done without OO, maybe it will be more clumsy but it can be done. Quote Link to comment Share on other sites More sharing options...
HauntedDawg Posted September 8, 2012 Share Posted September 8, 2012 OOP is actually Object-Oriented PROGRAMMING, not PHP. OOP can be usefull, it depends on what your doing, and what's your preference. Also, in time.php, line 3 should be; $otime = new time(); As you have a construct function, use it. I would like to point out, Do not use () if you are not passing any variables to it! And the Kohana Framework strongly suggests this as well. Quote Link to comment Share on other sites More sharing options...
Nickson Posted September 8, 2012 Share Posted September 8, 2012 As djkanna already stated, OOP can be a huge advantage as it's very easy to reuse code libraries or a set of functions. Not only might it speed up your development process (note: development process, OOP is not really faster). It should also make your code more readable, which is a huge plus imho. It can mean that you'll get a better project overview, but also know that using OOP can get very complex as well due to a load of nesting without any references in a file (most of the time you'll find something though. When writing procedural code, there is always a reference to another function that's called. This is not always true in OOP due to certain php features. What I do wonder though is where you learned this from? It looks to me that you might be using a rather outdated source. Not saying that it is bad, but why not do it properly from the start? Declaring variables with var is still possible, but I'd advise you to go with the public/private/protected[static/final]... at a later point, it gives you more options than just using var. Also, I'm not sure whether it's you that's doing the bad formatting, or the forum's syntax highlighter is at fault. Point is, keep it readable for yourself ;) but keep going! Quote Link to comment Share on other sites More sharing options...
Razor42 Posted September 9, 2012 Author Share Posted September 9, 2012 Nice to see what you all think. @Nickson. Yes I am currently doing some stuff on public/private/protected[static/final] but mainly public at the moment :). Quote Link to comment Share on other sites More sharing options...
Spudinski Posted September 9, 2012 Share Posted September 9, 2012 I agree with (a_b) that you can make reusable functions/methods purely with procedural programming techniques. It's actually not debatable. But, there are reasons why one would use OOP instead of Procedural - main reasons are inheritance(scope) and portability. You wouldn't want your DB authentication adapter on a global scope per-se, but you would want the query method to that database to be global. This is where public/private/protected/static keywords comes in. One thing I'd like to bring the most attention to is inheritance. I see millions of classes that gets it wrong, and it annoys the living moo out of me. This makes other developers lives very hard when they want to extend it or write an interface for it. But, if I can give you some advice towards learning PHP OOP: I highly suggest you start off with PHP4 OOP and go up from that. Will learning OOP make you a *better* programmer? Probably not. Will knowing how OOP works and being able to implement design patterns make you a *faster* programmer? Yes. Quote Link to comment Share on other sites More sharing options...
Nickson Posted September 12, 2012 Share Posted September 12, 2012 @spudinski, what's actually your view on the new php 5.4 traits? I have you looked into that yet? Quote Link to comment Share on other sites More sharing options...
a_bertrand Posted September 12, 2012 Share Posted September 12, 2012 For those which do not have a clue about what traits are (like me 5 min ago): http://simas.posterous.com/new-to-php-54-traits Seems a nice feature to have for me. Not sure how often I would use it but well it's there. Quote Link to comment Share on other sites More sharing options...
Spudinski Posted September 12, 2012 Share Posted September 12, 2012 @spudinski, what's actually your view on the new php 5.4 traits? I have you looked into that yet? As I see it, traits are another way of copying objects, nothing too new there, in JavaScript it's quite common grounds. I've set myself onto learning traits when PHP 5.4 first came out, but I haven't used them at all since. On the other hand, I use do use interfaces. As a side note, the link a_b posted doesn't really give a decent explanation of traits. I'll simplify it. Scenario: You have two main classes, database and player. You need to provide error logging capabilities to both of them (traits). Meanwhile, the database class have different adapters (interfaces). See zip: nand.co.za/downloads/oop_dev.zip Quote Link to comment Share on other sites More sharing options...
Aventro Posted September 16, 2012 Share Posted September 16, 2012 First of all, your example class sucks. Don't reinvent the wheel, there is an already great DateTime class, http://se2.php.net/manual/en/class.datetime.php You should consider using object-oriented code when you need it, not because you want to use it. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.