0

I started learning oop about 6 months ago, although I stopped for a while because of studies.

The problem is that whenever I use oop in my project I usually try to just make every single thing a class... Whether intialization, displaying, looping and much more... to be precise, I haven't gotten the concept of when to use/create classes.

Another problem is that I tend not to know the major benefit of a classes over a functions . Currently, I just mix them up.

I watch a youtube video that talks about classes and client code. In that video they said that classes are more of like developer codes while client codes are codes we write which uses the developer code to make things easier. So he advise we break our code to client/developer code. But I don't know how to break my code into client code and developer code. Infact at a point I was creating classes for all my pages like index.php, about.php

<?php
    class about{
        public $title = 'about page';
        public $page_color = 'red';
      //functions and more codes
  }

After writing the above code I know I am not doing something right. I have tried to search google and have read many oop books but each time they use the car class example which I can not apply to my coding or just wouldn't address the context were a class is or isn't necessary.

Note: I am comfortable building website using procedural codes but I need to learn oop incase I need it someday.

Stanley Aloh
  • 360
  • 4
  • 11
  • 1
    I don't know how much it helps, but I think the answers to [this](https://stackoverflow.com/questions/1301606/how-can-i-practice-better-object-oriented-programming#1301661) question are worth a read. – Federico klez Culloca Sep 05 '18 at 08:05
  • 2
    Given that you're using PHP and you're probably making a website, I can really suggest to just use a framework until you're more comfortable with this. Most frameworks will tell you _exactly_ how to build every step and what should and shouldn't be a class. – Evert Sep 05 '18 at 08:05
  • 1
    My tip is that you should learn about software architecture in general to understand one way to use classes in PHP. I'd highly recommend to check some MVC (model-view-controller) tutorials. For example: https://www.sitepoint.com/the-mvc-pattern-and-php-1/ – maio290 Sep 05 '18 at 08:06
  • @chris I try using oop to build my website which mostly are blogs, e-commerce and forums etc – Stanley Aloh Sep 05 '18 at 08:14
  • After wasting so many hours with pure php, and building my app, I realize that I should have used a framework from day one. Currently I use codeigniter, and it gives enough control over what is needed. – mlwn Sep 05 '18 at 08:15
  • OOP is mostly a way of keeping your code sensibly organised. There are 4 main principles that will get you there: warning excessive cursing in the link: https://lostinvim.com/blog/using-classes-is-not-oop-you-cunt/ – Royal Wares Sep 05 '18 at 08:17
  • I struggled with learning OOP, coming from an earlyish PHP background I grew up in code as PHP matured and have since picked up other languages and experience... my first revelation was writing my own database class that I could actually make use of in my site, rather than class whale extends animal examples I was reading up on at the time, "Damnit Jim I'm a coder not a vet" – Dale Sep 05 '18 at 08:19
  • 1
    The problem I spot here is that `About` is an isolated class. That means that your different pages do not share any common functionality, which is weird because they should be mostly the same thing. It'd be more useful to make in extend e.g. `Page`. In fact, you could probably just use `Page` everywhere and only extend it for very specific pages (and "about", which is probably a static page, is not a candidate for custom code). – Álvaro González Sep 05 '18 at 08:20
  • PHP is not the best language to learn `OOP` in at this point. I'm sure u'r better off with looking at `java` / `C#` / ... examples and implement those in `PHP` – DarkBee Sep 05 '18 at 08:22
  • 2
    @DarkBee that's not quite right, PHP is just as capable as Java and C# to implement OOP. It's always about the mindset of the developer not the language. – Royal Wares Sep 05 '18 at 08:45
  • @ÁlvaroGonzález I mean that `PHP` is way too forgiving environment, if you don't set types for your arguments and whatev you can just pass anything u'd like to every function/method. `PHP` should be stricter imho to be a good starting point to learn `OOP` – DarkBee Sep 05 '18 at 09:05

3 Answers3

0

To answer your question: Yes you're using it incorrectly. However it's not a quick fix to get you using it correctly.

There isn't an answer I can give you that will mean you start writing perfect OOP code tomorrow.

You're on the right path, study and practice. Make mistakes, live with those mistakes and refactor them. You'll get sharper the more you do this.

I'm at 16 years experience and I still find new rules and exceptions in OOP. It's not an exact science - it's a method that can yield different results depending on the context.

Just look at all the comments you've had and you'll see no general perfect consensus. Everyone is iteratively improving each others implementation. You can use this approach in your code, keep it tidy and organised, swap bits out as you find better or more correct ways of implementing them.

Royal Wares
  • 1,192
  • 10
  • 23
0

Well I have not learned php. But I am a good Java developer. And my OOP concepts are good. You mentioned that you do not understand major advantages of classes over function. Both of them are totally different. A class is a blueprint of software.Where as a function represents behaviour of software. You write all the code that does the processing of data provided as input by user and generation of output in functions. Classes contain these functions. The major advantage of writing code in classes is that they can be written in one place and reused anywhere. And OOP concepts are too vast. Each and everything cannot be explained in posts. OOP is much more advantageous than procedural-programming. Consider you are working on a very big project. Some features in the software are common. In OOP you can write code in one place and use them wherever you want.Whereas in procedural-programming you have to rewrite the same code wherever you are using those features. So, your headache of writing code will be minimized in OOP. Also, if the common code works on one place it will obviously work wherever it is being used. This is how OOP evolved from procedural-programming.

Karthik Pai
  • 587
  • 1
  • 9
  • 18
-1

OOP is the backbone of most programming languages so you will want to use it basically all the time when you have it fully understood.

Websites are a little trickier to give you good examples of so I'll try and give a generic outline and then a more web-based focus.

Classes should hold data that is related to each-over in a common sense name. For example a Car would obviously hold the Engine (another class), the colour, the name, the owner, etc. And within that the engine would hold it's own set of data that is relative to it (max speed, miles per gallon, etc.). This is the basis of holding your data together, next comes functions within classes.

Now while Car holds data relative to the car, Car also holds functions that the car would do, or functions that the car will use to interact with it's data. Getters(GetColour()) allow outside objects to get information about that specific car (as Car1 and Car2 could hold different colours). Setters are the opposite, to set object specific data. Then we have everything else. Car.start() which internally calls this.engine.start() which may set up some functions to start draining gas from the engine and allow the car to move.

Classes are all about having this easy to read, easy to understand, and easy to re-use. Once your Car class is complete you can re-use as many cars as you need and they are all nicely self-contained.

On to web

Firstly, I highly recommend you look into MVC architecture, which is where the bulk of web architecture is and makes OOP easier to understand for web. Basically your website is split into controllers (in charge of interracting with the DB and giving you back the page), models(Your database data, e.g a user) and your views (HTML with a little templating so your controllers can pass it dynamic data)

For example: You go to website/posts/Stanley , Your Router (another MVC thing) decides that this needs to be handled by the PostsController(a class). The posts controller knows that you want all posts by user Stanley, so it asks the Posts Model(a class)to retrieve all posts by Stanley, the controller then passes these to the view to fill out a template using your Stanley specific posts.

Going away from MVC a second,

With your example given, you will mostly use classes, I would assume, for data manipulation and DB access. For example if you stored blog posts in a database you may have a class Blog with data that matches your db rows (id, user_id, title, body) and functions to access that data to put in your HTML, but also to grab that from the database easily.

For example: BlogPost::All() may return an array of every blog post newest to oldest. BlogPost:By(user_id) may return an array of posts by a specific user. BlogPost:Remove(id) may remove the blog post given it's id. BlogPost:RatingAtLeast(starRating), get all blog posts with atleast a starRating rating.

You get the idea... This class would exist as you said above, to give you a client code way to access your database that is SPECIFIC to your use case and as BlogPost contains variables related to your BlogPost. So BlogPost::All() will return an array of BlogPost.

Prodigle
  • 1,757
  • 12
  • 23