Thread: An introduction to OOP in PHP [Made 4 years ago so sloppy as fudge but #tbt]

Page 1 of 2 12 LastLast
Results 1 to 10 of 12
  1. #1 An introduction to OOP in PHP [Made 4 years ago so sloppy and bad but helpful imo] 
    Banned
    Join Date
    Apr 2011
    Age
    29
    Posts
    1,360
    Thanks given
    370
    Thanks received
    184
    Rep Power
    0
    Let me explain, before I start, that this might be a little off. It's mostly community and feedback created, so yeah. I don't use OOP a lot, and am kind of new to it. The original tutorial was shit, but I've added a lot to it from feedback and articles. Credits are listed at the bottom of the page.

    OOP is mainly used for variable reusage and, as many people say, relates to real life situations so it's easier to understand.

    PHP is a multiple-paradigm language, meaning you can write it in either procedural or object orientated. The language itself is not fully object orientated, and you cannot make it so.

    So, in OOP, we usually have two files: the class, and then the handler. Let's get started with our class:

    class.php
    Code:
    <?php
    class OOP {
    public $variable;
    }
    ?>
    As you can see, the class name does not have to be the same as the file name (but it is highly recommended to be so), like how it is in java and many other programming languages. Also, notice how we defined our variable as a public status. This is because if you do not put anything before the variable, it gives you an error. Please note that there are a bunch of things you can put in front of your variable - it all really depends to be honest. They are called modifiers and modify how the variable is used. For some of the modifiers, please look at the bottom of the page. You may have also noticed that our variable is undefined. It can be defined or kept this way, it does not matter.

    Now, we need to define our variables. Well, if we did not define them straight away, then how do we do this? It's simple, and here's how:

    Code:
    <?php
    class OOP {
    public $variable;
    
    function __construct() {
    $this->variable = "Hello";
    }
    
    }
    ?>
    As you can see, we defined it using the $this-> method. Now, let me explain what $this-> means:

    In any programming language, "this" references to the class you are working in. If you attempt to do a "this" in a regular PHP file with no class, you will get an error as there is no object/class to reference the variable to. Also, we defined it in a function and not just out in the open. Why? Because we would have gotten a syntax error (explained below).

    * QUICK STOP NOTE *
    • Syntax Error: You would have received the "EXPECTING T_FUNCTION" error when using any function besides your own in a class
    • public function __contruct {} - This is a constructor (listed at the bottom of the page). A constructor is called whenever a object initializes as a result from a class.


    Now that we have defined our variable, how would we display it on the page? Well, we certainly wouldn't go straight out and echo it through the class. You can do this, but you would need a whole function, and then you would need to display the function. This isn't as neat, and personally, I like to have a handler:

    handler.php
    Code:
    <?php
    $OOP = new OOP();
    ?>
    Note: Your object should be named after your class. Read the next paragraph for information on objects!

    Now, as you can see, we are creating a new object named $OOP (objects are the result of initiating a class) and running it through our handler. If you run this, however, you will get an error. For all of you Java Guru's, this is not like Java anymore, and let me explain:

    In PHP, since classes do not have to be named after the file name, you must include the class first. In Java, however, all classes must be named after their filename. This is why in java you do not need to include the file, but only define it.

    So, to include our file (for those of you who don't know this), we would do this:

    [PHP]<?php
    require_once('class.php'); # once to prevent errors
    $OOP = new OOP();
    ?>[/PHP]

    Now it can fetch the class correctly.

    So, to echo our variable, we would then do this:

    Code:
    echo $OOP->variable; # Calls the variable $variable from the object $OOP (class: OOP)
    And Voila, you are done!

    Notes/References
    • You cannot echo or do anything of the sort in a class, you can only define variables and functions
    • OOP Can be very confusing to some people, so if you do not get this, I advise you to go read up on it more


    Definitions
    • Class: This is a programmer-defined datatype, which includes local functions as well as local data. You can think of a class as a template for making many instances of the same kind (or class) of object.
    • Object: An individual instance of the data structure defined by a class. You define a class once and then make many objects that belong to it. Objects are also known as instance.
    • Constructor: Refers to a special type of function which will be called automatically whenever there is an object formation from a class.


    Modifiers
    • Public: Accessible by everything and anything
    • Protected: Accessible only in the same class or classes that extend the class it is in
    • Private: Accessible ONLY in the class it is declared in


    Credits
    - Object Oriented Programming in PHP - A good-looking tutorial for OOP users. I got most of the definitions here, but none of my notes were from here. I haven't read it, so feedback would be appreciated
    - Eight : Really helped me with some additions and suggestions. Shoutout to him.
    Reply With Quote  
     

  2. #2  
    q.q


    Join Date
    Dec 2010
    Posts
    6,519
    Thanks given
    1,072
    Thanks received
    3,535
    Rep Power
    4752
    Quote Originally Posted by Belz View Post
    if you want OOP in PHP you're better off not using it at all lmao
    I don't understand this post. PHP is much easier and quicker to work with when using OOP.
    Reply With Quote  
     

  3. #3  
    q.q


    Join Date
    Dec 2010
    Posts
    6,519
    Thanks given
    1,072
    Thanks received
    3,535
    Rep Power
    4752
    Quote Originally Posted by Belz View Post
    PHP as a language is designed horribly and can hardly be called OOP
    That is true, however with any decent PHP framework the majority of PHP downfalls are resolved.
    Reply With Quote  
     

  4. #4  
    Banned
    Join Date
    Apr 2011
    Age
    29
    Posts
    1,360
    Thanks given
    370
    Thanks received
    184
    Rep Power
    0
    Quote Originally Posted by Belz View Post
    PHP as a language is designed horribly and can hardly be called OOP
    Something either is or isn't OOP. You can't have an in between for the most part. Yes, PHP can be either or, but Objects are handled (for the most part) in the same way. Which is the point of OOP PHP.
    Reply With Quote  
     

  5. #5  
    Registered Member

    Join Date
    Nov 2013
    Posts
    746
    Thanks given
    187
    Thanks received
    459
    Rep Power
    5000
    HTML5 4 ever

    Attached image


    Attached image

    Reply With Quote  
     

  6. #6  
    Respected Member


    Join Date
    Jan 2009
    Posts
    5,743
    Thanks given
    1,162
    Thanks received
    3,603
    Rep Power
    5000
    this is a rather piss poor introduction to oopand you should most likely get up to date with php before making tutorials about it specially in regards oop as a lot has changed in php over the last couple of years
    Reply With Quote  
     

  7. #7  
    Registered Member
    Anthony`'s Avatar
    Join Date
    Sep 2008
    Age
    29
    Posts
    763
    Thanks given
    75
    Thanks received
    164
    Rep Power
    204
    I suggest reading up on encapsulation while you're at it.
    Reply With Quote  
     

  8. #8  
    Banned
    Join Date
    Apr 2011
    Age
    29
    Posts
    1,360
    Thanks given
    370
    Thanks received
    184
    Rep Power
    0
    Quote Originally Posted by Anthony` View Post
    I suggest reading up on encapsulation while you're at it.
    I'm a developer who has graduated high school for web development and computer science; I know what encapsulation is, and I know this tutorial is bad. It's not supposed to teach one how to use PHP, but rather, understand what OOP is. Classes create objects, and those objects have specific purposes.
    Reply With Quote  
     

  9. #9  
    Registered Member

    Join Date
    Jan 2013
    Posts
    485
    Thanks given
    58
    Thanks received
    132
    Rep Power
    248
    Quote Originally Posted by OpTec FrAgSHoT View Post
    I'm a developer who has graduated high school for web development and computer science; I know what encapsulation is, and I know this tutorial is bad. It's not supposed to teach one how to use PHP, but rather, understand what OOP is. Classes create objects, and those objects have specific purposes.
    So a better title for this thread would be "Introduction to Object-Oriented Programming, using PHP syntax"?
    Reply With Quote  
     

  10. #10  
    Registered Member
    Anthony`'s Avatar
    Join Date
    Sep 2008
    Age
    29
    Posts
    763
    Thanks given
    75
    Thanks received
    164
    Rep Power
    204
    Quote Originally Posted by OpTec FrAgSHoT View Post
    I'm a developer who has graduated high school for web development and computer science; I know what encapsulation is, and I know this tutorial is bad. It's not supposed to teach one how to use PHP, but rather, understand what OOP is. Classes create objects, and those objects have specific purposes.
    Am I supposed to be impressed you graduated high school? This tutorial clearly showed me you don't understand it properly still because encapsulation is language-agnostic. It doesn't just apply to PHP but the object-oriented paradigm itself. This forum would be better off if you didn't post such awful tutorials, and what's even worse is the fact you don't even want to fix it. If this is what you're going to do, just don't bother. Sorry.
    Reply With Quote  
     

Page 1 of 2 12 LastLast

Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. How do I get an interface to fade in or out?
    By Snow Cat123 in forum Help
    Replies: 7
    Last Post: 10-19-2013, 12:31 PM
  2. Replies: 9
    Last Post: 02-05-2013, 10:26 AM
  3. Touhou 101: An introduction to touhouism
    By Death Grips in forum Anime
    Replies: 3
    Last Post: 05-21-2012, 02:45 PM
  4. An Introduction to JAVA: The Basics.
    By `Dell in forum Application Development
    Replies: 18
    Last Post: 01-12-2011, 03:21 AM
  5. getting the day of the year in php
    By fabjan in forum Application Development
    Replies: 5
    Last Post: 06-20-2008, 03:50 PM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •