Results 1 to 5 of 5
  1. #1
    Junior Member
    Join Date
    May 2015
    Location
    Cuba
    Posts
    0

    An Introduction to Object Oriented Programming

    Do not respond to this unless you have questions about the content of the examples. "Thank you" posts and general questions about OOP are not welcome on this thread

    As requested by some members, and intended to be a useful resource to add to, here is an introduction to OOP.

    This is by no means complete and is intended to help those who just don't get objects to think about things a little differently, and slowly introduce new concepts.

    The next installment will look at object inheritance.


    Pre-requisites

    • You currently write and understand PHP sufficiently well enough to know what variables and functions are in PHP
    • You are aware that PHP supports something called "objects" and that there is a concept called "Object Oriented Programming (OOP)"



    These posts will help you build on your current understanding of PHP towards understanding what objects are and how they are used in a PHP programming environment.

  2. #2
    Junior Member
    Join Date
    May 2015
    Posts
    0

    An Introduction to Object Oriented Programming

    Object Orientated PHP

    If you are approaching objects from a procedural or functional background then the concept of an object in a programming context can seem confusing, bewildering, backwards... or any combination thereof.

    In essence PHP is a procedural language with support for functions and objects. It is also a single threaded language so for every request a procedure begins, executes a set of commands, line by line, based on the code and then finishes.

    Part of the object confusion is following the procedure. If you're trying to learn by inspection then you'll often finding your way jumping from file to file looking at functions inside classes using other objects in other files. This will drive you crazy and attempting to learn from the beginning in this way will quite often lead to headaches, frustration and resentment of OO principles.

    Ideally you'll grasp the OO basics and principles, and then you'll start learning by inspection, testing and other sources, eg the PHP manual, PHP forums, etc. If you understand the concept, the syntax and the principles then you can move forward.

    OO Nomenclature (naming):

    Functions inside classes are called methods
    Variables inside classes are called properties (sometimes also called attributes or fields)
    A 'class' is the code you write, an 'object' is what you get when you use a class

  3. #3
    Junior Member
    Join Date
    May 2015
    Posts
    0

    An Introduction to Object Oriented Programming

    This code defines a class. When we do "new A" we get an object of class A or, in other words, "an instance of A".

    At this point it might be worth looking at some dictionary definitions of "class" to see why the term is used

    "Class":


    A set or category of things having some property or attribute in common and differentiated from others by kind, type, or quality.


    Assign or regard as belonging to a particular category:

    This starts to make sense a little bit until we look at the term object

    "Object":


    A material thing that can be seen and touched.

    Huh? Hopefully this is what this article will answer!

    "Instance":


    An example or single occurrence of something

    Classes and Objects help you divide up and manage your code better. At a basic level they offer an extended scope and can reduce copying and pasting of code - ie you can improve your code reuse.

    All OO languages have something called the "object operator". in php this is "->", eg

  4. #4
    Junior Member
    Join Date
    May 2015
    Location
    Cambodia
    Posts
    0

    An Introduction to Object Oriented Programming

    An Introduction to Object Oriented Programming


    Do not respond to this unless you have questions about the content of the examples. "Thank you" posts and general questions about OOP are not welcome on this thread

    As requested by some members, and intended to be a useful resource to add to, here is an introduction to OOP.

    This is by no means complete and is intended to help those who just don't get objects to think about things a little differently, and slowly introduce new concepts.

    The next installment will look at object inheritance.


    Pre-requisites

    • You currently write and understand PHP sufficiently well enough to know what variables and functions are in PHP
    • You are aware that PHP supports something called "objects" and that there is a concept called "Object Oriented Programming (OOP)"



    These posts will help you build on your current understanding of PHP towards understanding what objects are and how they are used in a PHP programming environment.


    Object Orientated PHP

    If you are approaching objects from a procedural or functional background then the concept of an object in a programming context can seem confusing, bewildering, backwards... or any combination thereof.

    In essence PHP is a procedural language with support for functions and objects. It is also a single threaded language so for every request a procedure begins, executes a set of commands, line by line, based on the code and then finishes.

    Part of the object confusion is following the procedure. If you're trying to learn by inspection then you'll often finding your way jumping from file to file looking at functions inside classes using other objects in other files. This will drive you crazy and attempting to learn from the beginning in this way will quite often lead to headaches, frustration and resentment of OO principles.

    Ideally you'll grasp the OO basics and principles, and then you'll start learning by inspection, testing and other sources, eg the PHP manual, PHP forums, etc. If you understand the concept, the syntax and the principles then you can move forward.

    OO Nomenclature (naming):

    Functions inside classes are called methods
    Variables inside classes are called properties (sometimes also called attributes or fields)
    A 'class' is the code you write, an 'object' is what you get when you use a class

    PHP Code:
    //this is a class
    class A {
    /* code */
    }

    //$a is an object "of class A"
    $a = new A;


    This code defines a class. When we do "new A" we get an object of class A or, in other words, "an instance of A".

    At this point it might be worth looking at some dictionary definitions of "class" to see why the term is used

    "Class":


    A set or category of things having some property or attribute in common and differentiated from others by kind, type, or quality.


    Assign or regard as belonging to a particular category:

    This starts to make sense a little bit until we look at the term object

    "Object":


    A material thing that can be seen and touched.

    Huh? Hopefully this is what this article will answer!

    "Instance":


    An example or single occurrence of something

    Classes and Objects help you divide up and manage your code better. At a basic level they offer an extended scope and can reduce copying and pasting of code - ie you can improve your code reuse.

    All OO languages have something called the "object operator". in php this is "->", eg


    PHP Code:
    //this is a class
    class A {
    function
    foo() {
    echo
    "Hello World";
    }
    }

    //$a is an object "of class A"
    $a = new A;

    //here we access one of the object's methods
    $a->foo(); //outputs "Hello World"


    in this example $a is an object, to access it's method "foo", we use the object operator, ->. To the left is an object, to the right is one of the object's methods or properties

    When writing a class, you'll be thinking about how the class behaves as an object. You'll be assuming something else has created an instance of your object and the flow of logic and control within use. When coding like this you'll want to access other methods or properties within your class. To do this you assume the variable "$this" as the current instance of the object, so you may do something like this


  5. #5
    Administrator
    Join Date
    May 2015
    Posts
    1,143
    An Introduction to Object Oriented Programming


    Do not respond to this unless you have questions about the content of the examples. "Thank you" posts and general questions about OOP are not welcome on this thread

    As requested by some members, and intended to be a useful resource to add to, here is an introduction to OOP.

    This is by no means complete and is intended to help those who just don't get objects to think about things a little differently, and slowly introduce new concepts.

    The next installment will look at object inheritance.


    Pre-requisites

    • You currently write and understand PHP sufficiently well enough to know what variables and functions are in PHP
    • You are aware that PHP supports something called "objects" and that there is a concept called "Object Oriented Programming (OOP)"



    These posts will help you build on your current understanding of PHP towards understanding what objects are and how they are used in a PHP programming environment.


    Object Orientated PHP

    If you are approaching objects from a procedural or functional background then the concept of an object in a programming context can seem confusing, bewildering, backwards... or any combination thereof.

    In essence PHP is a procedural language with support for functions and objects. It is also a single threaded language so for every request a procedure begins, executes a set of commands, line by line, based on the code and then finishes.

    Part of the object confusion is following the procedure. If you're trying to learn by inspection then you'll often finding your way jumping from file to file looking at functions inside classes using other objects in other files. This will drive you crazy and attempting to learn from the beginning in this way will quite often lead to headaches, frustration and resentment of OO principles.

    Ideally you'll grasp the OO basics and principles, and then you'll start learning by inspection, testing and other sources, eg the PHP manual, PHP forums, etc. If you understand the concept, the syntax and the principles then you can move forward.

    OO Nomenclature (naming):

    Functions inside classes are called methods
    Variables inside classes are called properties (sometimes also called attributes or fields)
    A 'class' is the code you write, an 'object' is what you get when you use a class

    PHP Code:
    //this is a class
    class A {
    /* code */
    }

    //$a is an object "of class A"
    $a = new A;


    This code defines a class. When we do "new A" we get an object of class A or, in other words, "an instance of A".

    At this point it might be worth looking at some dictionary definitions of "class" to see why the term is used

    "Class":


    A set or category of things having some property or attribute in common and differentiated from others by kind, type, or quality.


    Assign or regard as belonging to a particular category:

    This starts to make sense a little bit until we look at the term object

    "Object":


    A material thing that can be seen and touched.

    Huh? Hopefully this is what this article will answer!

    "Instance":


    An example or single occurrence of something

    Classes and Objects help you divide up and manage your code better. At a basic level they offer an extended scope and can reduce copying and pasting of code - ie you can improve your code reuse.

    All OO languages have something called the "object operator". in php this is "->", eg


    PHP Code:
    //this is a class
    class A {
    function
    foo() {
    echo
    "Hello World";
    }
    }

    //$a is an object "of class A"
    $a = new A;

    //here we access one of the object's methods
    $a->foo(); //outputs "Hello World"


    in this example $a is an object, to access it's method "foo", we use the object operator, ->. To the left is an object, to the right is one of the object's methods or properties

    When writing a class, you'll be thinking about how the class behaves as an object. You'll be assuming something else has created an instance of your object and the flow of logic and control within use. When coding like this you'll want to access other methods or properties within your class. To do this you assume the variable "$this" as the current instance of the object, so you may do something like this


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •