Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Mar 2005
    Posts
    855
    Thanks
    7
    Thanked 1 Time in 1 Post

    What's the importance in creating objects

    What's the importance in creating objects as opposed to simply creating variables or arrays and echo-ing them?

    https://secure.php.net/manual/en/lan...pes.object.php
    My youtube channel:
    https://www.youtube.com/channel/UCme...H42czlwGaBX8JA

    Compare bible texts (and other tools):
    The Biblewheel

  2. #2
    Master Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    5,842
    Thanks
    26
    Thanked 609 Times in 602 Posts
    Objects contain datasets (a set of related values) and store it in a single variable. And objects let you choose the behaviour a certain dataset can have.

    simple example:
    PHP Code:
    class Person
    {
        public 
    $firstName;
        public 
    $lastName;

        public function 
    __construct($first$last)
        {
            
    $this->firstName $first;
            
    $this->lastName $last;
        }

        public function 
    getFullName()
        {
            return 
    $this->firstName ' ' $this->lastName;
        }

    with this simple object you have both first and last name together (you can't lose one of them by accident) and you only have only one place where the full name is created (saving you the hassle of creating it every time you need the full name).

    ... as opposed to simply creating variables ...
    Objects are variables. Data-wise, pretty much anything that is not a constant is a variable.

    ... or arrays and echo-ing them?
    Why would you echo an object? That's what objects have methods for.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  3. #3
    Senior Coder deathshadow's Avatar
    Join Date
    Feb 2016
    Location
    Keene, NH
    Posts
    3,568
    Thanks
    5
    Thanked 516 Times in 503 Posts
    Quote Originally Posted by Dormilich View Post
    Why would you echo an object? That's what objects have methods for.
    Because of that, I'm not even sure the entire question makes sense. I literally have no clue what is even being asked here.
    “There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies.” – C.A.R. Hoare, The 1980 ACM Turing Award Lecture
    http://www.cutcodedown.com

  4. #4
    Regular Coder
    Join Date
    Mar 2005
    Posts
    855
    Thanks
    7
    Thanked 1 Time in 1 Post
    Why not make an array of arrays instead of an object?
    My youtube channel:
    https://www.youtube.com/channel/UCme...H42czlwGaBX8JA

    Compare bible texts (and other tools):
    The Biblewheel

  5. #5
    Senior Coder deathshadow's Avatar
    Join Date
    Feb 2016
    Location
    Keene, NH
    Posts
    3,568
    Thanks
    5
    Thanked 516 Times in 503 Posts
    Quote Originally Posted by gilgalbiblewhee View Post
    Why not make an array of arrays instead of an object?
    What are you doing? What's the data? What's your scoping model? How are you handling and avoiding unnecessary locals?

    You're not asking / providing enough for us to even come close to answering.
    “There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies.” – C.A.R. Hoare, The 1980 ACM Turing Award Lecture
    http://www.cutcodedown.com

  6. #6
    Regular Coder
    Join Date
    Mar 2005
    Posts
    855
    Thanks
    7
    Thanked 1 Time in 1 Post
    Well I wanted general information. My knowledge in php is very basic of if statements, for loops, pdo. But things like these I don't understand. Nor why within functions brackets certain variables (?) are inserted like myfunction($variable1, $variable2, $variable3)?
    My youtube channel:
    https://www.youtube.com/channel/UCme...H42czlwGaBX8JA

    Compare bible texts (and other tools):
    The Biblewheel

  7. #7
    Senior Coder deathshadow's Avatar
    Join Date
    Feb 2016
    Location
    Keene, NH
    Posts
    3,568
    Thanks
    5
    Thanked 516 Times in 503 Posts
    Quote Originally Posted by gilgalbiblewhee View Post
    But things like these I don't understand. Nor why within functions brackets certain variables (?) are inserted like myfunction($variable1, $variable2, $variable3)?
    Ok, what I think you are missing is the concept of SCOPE.

    As projects get larger and larger you start to run out of names for things. It starts to get very convoluted when you cannot re-use the same name when the variable name describes what something IS. This is even more of a problem if you create libraries or includes -- or use other people's code -- where they may be trying to use the same variables as your main code.

    Likewise the more 'named' entities you have in the global scope in an interpreted language (like PHP) the slower the language's execution gets. You end up with a larger and larger table of names it has to turn into memory offsets (typically a 32 or 64 bit integer number pointing at a physical memory location) making it work harder and harder the more stuff you declare.

    Memory use is also an issue since it gets annoying to constantly have to unset/null your variables if they are all in the global scope. This is PART of why globals are often avoided as much as possible.

    Scope eliminates that problem. When you declare a variable inside a function, it only exists for that function.

    Code:
    function test() {
      $test = 'tarfu snafu fubar';
      echo $test, '<br>';
    }
    
    test();
    
    echo 'Is test set? ', isset($test) ? 'yes' : 'no', '<br>';
    $test only exists inside test() because that is where it is first declared. If we were to change it to this:

    Code:
    $test = 'fubar';
    
    function test() {
      $test = 'snafu';
      echo $test, '<br>';
    }
    
    test(); // outputs 'snafu'
    echo $test, '<br>'; // still outputs fubar
    You can see that the $test inside the function is an entirely separate variable from the global one. To access a global in PHP you have to come right out and SAY you want access to it. This can be considered something of a security mechanism.

    Code:
    $test = 'fubar';
    
    function test() {
      global $test;
      $test = 'snafu';
      echo $test, '<br>';
    }
    
    test(); // outputs 'snafu'
    echo $test, '<br>'; // also outputs 'snafu'
    Declaring variables in a function like "function ($a, $b)" is how you pass values to a function without opening up the global scope. That's the core of how EVERY programming language works and if you're this far into it you've been using said mechanism without even realizing it.

    Let's use as an example a simple template function to output a menu line. Rather than typing all the markup for every LI/A/href you could just:

    Code:
    function menuItem($href, $text) {
      echo '
        <li><a href="', $href, '">', $text, '</li>';
    }
    
    menuItem('/', 'Home');
    menuItem('/forums', 'Forums');
    menuItem('/contact', 'Contact');
    The advantage to this approach being that if you want to change the markup of ALL anchors you only have to edit it in one spot, not for each and every menu item. If we didn't have that arguments mechanism, well... this would suck:

    Code:
    function menuItem($href, $text) {
      global $href, $text;
      echo '
        <li><a href="', $href, '">', $text, '</li>';
    }
    
    $href = '/';
    $text = 'Home';
    menuItem();
    $href = '/forums';
    $text = 'Forums';
    menuItem();
    $href = '/contact';
    $text = 'Contact';
    menuItem();
    Particularly when you have no idea if something else in your code wants to use variables named $href and $text -- using globals like that could screw over some other part of the code.

    Parameters/arguments on functions also help with memory management. As they only exist inside the function for that function, the system memory used by them can be reclaimed for other purposes when the function ends. This cleanup of memory is called "garbage collection" and varies from language to language in implementation. In proper compiled languages like C and Pascal where things are strictly typecast it's as simple as restoring the stack pointer to where it was before the function was called -- a single operation built into the return statement. In interpreted and non strictly typecast languages garbage collection most always involves more complex memory management on the 'heap' -- a complex topic best left for another time.

    Objects are LIKE associative arrays, but they exist for two reasons. 1) most other programming languages don't even HAVE associative arrays (where an array can use a name instead of an index number), and 2) security protection mechanisms to further 'stiffen' the scope without variables/parameters being destroyed.

    The concept of scope coming to the fore in PHP's implementation of objects with the keywords:

    public -- all routines can access it.

    private -- visible within the object itself only.

    protected -- visible to all classes that extend or were parents of the current class.

    Those three keywords can be applied when you declare a property (variable) or method (function) inside a Class to declare how and where those things are available.

    Though that's a complex topic best left for another time.
    “There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies.” – C.A.R. Hoare, The 1980 ACM Turing Award Lecture
    http://www.cutcodedown.com

  8. Users who have thanked deathshadow for this post:

    gilgalbiblewhee (Jul 28th, 2018)

  9. #8
    Regular Coder
    Join Date
    Mar 2005
    Posts
    855
    Thanks
    7
    Thanked 1 Time in 1 Post
    so scope is perhaps bigger than a function. And variables can be redefined with the global declaration.
    My youtube channel:
    https://www.youtube.com/channel/UCme...H42czlwGaBX8JA

    Compare bible texts (and other tools):
    The Biblewheel


 

Tags for this Thread

Posting Permissions

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