HiveBrain v1.2.0
Get Started
← Back to all entries
patternphplaravelMinor

Simple application to experience encapsulation

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
simpleexperienceencapsulationapplication

Problem

Looking at the code, is this a proper example of encapsulation?

additionalComputer.php (class)

<?php
/** Counts up the Number of Additional PC Options
* and stores them into an array then sends them to the view.
*/
    class additionalComputer {
        protected function displayMenu() {
            $addtpcs= [];
            for ($i=0; $i <= 100; $i++) {
            $addtpcs[$i] = $i;
            }
        return $addtpcs;
        }

    }


additionalPCs.php (Class)

displayMenu();    
    }
}


IndexController.php (Controller)

display();
        $this->layout->content = View::make('index')->with('addtpcs', $addtpcs)->with('businesstypelist', businesstype::dropdown())->with('contracttermlist',ContractTerm::dropdown());
    }


Would you provide me with a "yes you are correct," "good but you forgot X," or "no you are screwed up and this is why"?

Here is another example I just wrote for someone of what I believe to be using encapsulation. They requested to be able to change their meta tags based upon the page name. Is this using proper encapsulation? This was the very first OOP programming script that I have actually written after everything that I have read and studied.

```
/**
* The very first thing that you want to do is to get your page name. To do this we are going to create two classes and a couple of methods within those classes.
* The next thing you want to do is send that pagename to another class for returning your meta keywords. To do this we are going to create two additional classes and a couple of methods within those classes.
* The third thing you are going to want to do is to instanitate the classes on the page and get the information.
* Note: If you are using an MVC Framework like Laravel then you will want to remove the ?>.
* Note 2: There are two different classes for security. The protected function ensures that only the class itself can change the method within the class. This is why we have to use a retrieval clas

Solution

Encapsulation is such an old term from the C++ days. Nowadays it's more SOLID principles that you should build your PHP code on.

I'm going to say it in 2 different ways.

-
Yes, it takes a bit of sense of Encapsulation. However, it's very messy as an example. The best example would have been better with a student with its properties and a simple index.php that calls it and set the object with properties and then you call get functions to retrieve the attributes.

-
Encapsulation in a OOP view is basically information hiding. So that's why I said yes/semi you are ok with what you have done, however the entire structure behind it doesn't show OOP itself with an end goal for any of us to actually say "no you are wrong" because you achieve something that should be done as a pure loop inside a single file. There is no object definition to create a class to show this. I am aware after explaining it to Marc that what you want to do is to loop and display but you don't need a class for that and using a class as an example for encapsulation to do a for loop example will just confuse yourself in the long run.

Context

StackExchange Code Review Q#43257, answer score: 3

Revisions (0)

No revisions yet.