Jump to content
MakeWebGames

Is Object-Oriented PHP (OOP) A Effective Code & Will It Improve My Coding?


Razor42

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

@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

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...