2

Again here....

I'm trying to learn PHP OOP (like in the last post), but as much as I read, I can not find the use of classes, methods and properties very well. So at the moment I don't find any sense to all of this. So I've thought it would be good to try to do an easy script using OOP to find why OOP is useful.

Would be the right exercise to do an script to calculate areas? Like... you choose circle, square or rectangle, then you add the height, width or ratio and it gives to the area in cm2. Would it be a script to practise OOP?

I'm so confused!!!! And internet is confusing me even more! If this is not a good idea... could anyone tell me a script where I can see I need to create properties.... initiate classes.... or mainly, can anyone suggest what script I could do being easy to follow and do step by step?

I've been practising with

class Cars {
    public $color
    public $brand
    public $doors
} 

To see how __construct, destruct and $this work, but I don't know what to do with it.... I mean.... in what case could I need to do this?

Thank you!

Almazik G
  • 1,066
  • 1
  • 9
  • 21
eve_mf
  • 795
  • 3
  • 12
  • 36
  • 2
    Hi. OOP is very powerful, and even though at the moment you dont see the importance of its use, you will once you deal with bigger applications or projects. Do not give up on it. A real software/web developer will know OOP and thats where you want to be. Businesses nowadays will only hire you if you know OOP so please dont give up – CodeGodie Aug 17 '15 at 13:21
  • Your programming model depends on your purposes. While you're just dealing with procedures and linear action scripting, procedural model will fit you just fine. When it comes the time that you have to deal with entity abstraction on your program, when you have to represent real life objects and behaviors on that, you will have OOP on your side to hit it. – al'ein Aug 17 '15 at 13:24
  • Basically think about repetition and spaghetti code. With classes and functions you, as a developer, would not have too much problem finding the piece of code you are looking for. Imagine having to debug a problem with Facebook. By having a break down of how the classes and functions are placed and created, you will do this with ease. – CodeGodie Aug 17 '15 at 13:24
  • 1
    The basics are all in the manual http://php.net/manual/en/language.oop5.php. – David Soussan Aug 17 '15 at 13:24
  • It's difficult to don't give up when I don't find any sense of what I'm reading or seeing on the screen. Could you tell me any easy/basic script to build that I can use OOP? Like the one I said with cars... or... animals... but then... what do I do with it? – eve_mf Aug 17 '15 at 13:24
  • 1
    i have some time to get on Teamviewer. If you like we can work together remotely on a small app so I can show you what youre not understanding. Let me know. – CodeGodie Aug 17 '15 at 13:25
  • Take a look at the PHP learning path of that site: https://www.codecademy.com/ . Jump lessons to 'Programming OOP' and 'Classes', it'll cover why it's used and how it'll help you on a very basic and friendly language. There are a lot of excellent books that cover it also, like the ones by O'Reilly and Packt. – al'ein Aug 17 '15 at 13:28
  • try this tutorial, where can have brief description of OOPS, http://www.phpro.org/tutorials/Object-Oriented-Programming-with-PHP.html –  Aug 17 '15 at 13:32
  • CodeGodie it would be super amazing!!!! I will get TeamViewer when I get home and I get back in touch! – eve_mf Aug 17 '15 at 13:33
  • yea no prob. I will leave my email here. Let me know when you get it, so I can delete this comment. Then you can email me when you're ready, lokazo8@yahoo.com By the way, at what time do you think this will happen so I can check my email? – CodeGodie Aug 17 '15 at 13:45
  • another great reference for coding standards [PHP The Right Way](http://www.phptherightway.com/), including OOP syntax and best practices – ramabarca Aug 17 '15 at 15:27

2 Answers2

1

OOP in PHP is a really easy and convenient way to write your application in PHP. You need to learn about class, objects, instance, interface, trait, abstract class and methods, static methods, magic methods etc.

Classes are bunch of methods and properties. Methods are almost like functions. properties are like variables.

There are some way to access properties and methods. In a class you can set public,private and protected methods and properties. You can access public methods and properties any where in your application. You can access private methods and properties only inside that class it resides and You can access protected methods and properties inside base class and child class.

In short learning OOP style is really fun. and it is so easy and efficient way to write application. php.net is really a great source to learn OOP style. This is the link PHP OOP

And there are also a lot of books on internet on PHP OOP style. Hope this will help.

Crunch Much
  • 1,537
  • 1
  • 11
  • 14
0

One of the "best" examples when OOP is way easier than procedural is a nifty structure like a tree.

Trees are quite common structure (like navigation menus). To create a tree with infinite depth the procedural way is "hard" while using OOP it's simple.

Secondly you can easily hide certain functionality to the scope of an object. (i.e. public vs private vs protected).

Using cars / animals etc etc is usually useful when interfaces / subclasses come into play.

A too simple Tree as an example

class Tree {
    private $parent = null;
    private $nodes = array();

    private $data;

    function __construct($data) {
        $this->setData($data);
    }

    function setData($data) {
        $this->data = $data;
    }

    function getData() {
        return $this->data;
    }

    function getNode($index) {
        return (isset($this->nodes[$index]) ? $this->nodes[$index] : null);
    }

    function addNode(Tree $node) {
        $this->nodes[] = $node;
    }

    function getParent() {
        return $this->parent;
    }

    function isRoot() {
        return ($this->parent === null);
    }

    function getRoot() {
        return ($this->isRoot() ? $this : $this->getParent()->getRoot());
    }
}
Ronald Swets
  • 1,669
  • 10
  • 16
  • Uuuh it's to complicated for me to understand it :( My comprehension is pretty bad... I need to read things a thousand times... and I usually understand it better with stupid examples like animas {elephant, cat, dog...} than if I start to read Node....Root....index... – eve_mf Aug 17 '15 at 14:57