Classes php

For discussions about game development that does not fit in any of the other topics.
Post Reply
User avatar
Chris
Posts: 1581
Joined: Wed Sep 30, 2009 7:22 pm

Re: Classes php

Post by Chris »

OOP is a means of structuring programs as "Objects" it stands for Object Oriented Programming.

The concept is that everything is an object, think about it. In real life, everything we give a name, has a border around it. That's how we can differentiate a coffee cup for example from a keyboard. And the keys on the keyboard from the keyboard. We give every different object we see a different name. That way we can tell each other what we are seeing and making use of.

OOP requires a unique kind of mind to understand it, most people can develop the skills however the people who understand OOP are people who are very very technically minded. In my opinion, once you understand OOP you understand how humans perceive the universe we live in.

http://php.net/manual/en/language.oop5.php
Fighting for peace is declaring war on war. If you want peace be peaceful.
Darius
Posts: 19
Joined: Fri May 10, 2013 10:26 am

Re: Classes php

Post by Darius »

They are two different approaches.
I like the example in following SO question: http://stackoverflow.com/questions/7592 ... -and-oo-pr
Employing an OOP style has significant advantages over procedural programming, especially as your scale increases. Consider the savings we would receive from OOP in terms of repeated code, flexibility, and maintenance if we also had to add forms for boats, motorcycles, planes, go-karts, ATVs, snowmobiles, etc. Objects and methods are also far easier to test than procedural programming by using unit testing to test results.
source:
http://www.virtuosimedia.com/dev/php/pr ... amming-oop
• Procedural Programming



– top down design

– create functions to do small tasks

– communicate by parameters and return values



• Object Oriented Programming



– design and represent objects

– determine relationships between objects

– determine attributes each object has

– determine behaviours each object will respond to

– create objects and send messages to them to use or manipulate their attributes
source:
http://ecomputernotes.com/java/what-is- ... ng-in-java
User avatar
Chris
Posts: 1581
Joined: Wed Sep 30, 2009 7:22 pm

Re: Classes php

Post by Chris »

Code: Select all

<?php
class Car {

    public $wheels = 4
    public $topSpeed = 250;

    public $make;
    public $model;

    function __construct($make, $model)
    {
        $this->make = $make;
        $this->model = $model;
    }

    function toString()
    {
        return $this->make . ' ' . $this->model;
    }
}

$civic = new Car('Honda', 'Civic');

echo $civic->toString(); // Honda Civic
echo $civic->topSpeed; // 250   
Fighting for peace is declaring war on war. If you want peace be peaceful.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Classes php

Post by Jackolantern »

OOP in JS is completely different. JS is not even a classical OOP language. Rather, JS is Prototype-Based.

The reason why you would use OOP is because it provides a much better structure for very large applications. When you are just working with functions in procedural programming, all of your data has to be "loose" and intermingling with other data. In OOP, you are making little units with your data and attaching the behaviors that can be performed on it. You also can control access. If something in your program is sensitive, you can strictly control what can access it and in what way. There really is nothing similar in procedural programming. OOP is also great with code re-use. In OOP, you don't care or have to care how an object does the work it does. All you have to know is its interface (how to interact with it) and what it returns. This makes OOP classes "black boxes" that you can pick up and drop into any application. Code libraries really exploded when OOP became popular in the early 80's because code re-use works so well with OOP.

I am a big fan of OOP and use it for pretty much everything except the most trivial 10 or 15 line application. :cool: EDIT: Well, in JS I am not always rocking the OOP because OOP is so strange there. I try to use the Module Pattern for fairly large JS scripts, but it doesn't always fit.
The indelible lord of tl;dr
User avatar
Chris
Posts: 1581
Joined: Wed Sep 30, 2009 7:22 pm

Re: Classes php

Post by Chris »

Oroton wrote:I don't get why you would use classes in php, seems like more work to do something with out classes.
In saying that it looks very similar to JS
You're daring to be skeptic about a proven method. A programmer should never limit themselves to one programming language or technique. Otherwise you'll never master any language.

You're working with software that is probably nearly 80% object oriented, why not take the effort to learn how it works. I'd advise you to work with some C#, you can't get any more strictly Object Oriented than that.
Fighting for peace is declaring war on war. If you want peace be peaceful.
Post Reply

Return to “General Development”