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)
<?php
class additionalPCs extends additionalComputer {
public function display() {
return $this->displayMenu();
}
}
IndexController.php (Controller)
<?php
class IndexController extends \BaseController {
Protected $layout = 'master';
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
/** Wayne - 03-02-2014 - Moved for loop to a method within its own class. */
$numberofpcs = new additionalPCs();
$addtpcs=$numberofpcs->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 class and method called a getter. This ensures that nobody from outside the function can change the original purpose of the method.
* Note 3: There are 5 different files in this one document.
*/
<?php
/**
* Class 1 named class.pageName.php
* This holds the protected method and returns the current page name. This is a page by itself.
*/
class pageName {
protected function curPageName() {
return substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);
}
}
?>
<?php
/**
* Class 2 named class.getPageName.php
* This calls the protected method in the first class and gets the URL from it
*/
require_once('class.pageName.php');
class getPageName extends pageName {
public function getName() {
return $this->curPageName();
}
}
?>
<?php
/**
* Class 3 named class.metaKeywords.php
* This takes the page name from the previous class and then outputs what keywords you want based upon the switch case statement.
*
*/
class metaKeywords {
private $metakeywords;
protected function meta($url) {
switch($url) {
case "keywordset1.php":
$metakeywords = "My, first, set, of, keywords";
break;
case "keywordset2.php":
$metakeywords = "My, second, set, of, keywords";
break;
default:
$metakeywords = "This,is,default";
}
return $metakeywords;
}
}
?>
<?php
/**
* Class 4 named class.getMetaKeywords.php
* This retrieves the information from the protected function of metaKeywords.
*/
require_once('class.metaKeywords.php');
class getMetaKeywords extends metaKeywords {
public function getKeywords($url) {
return $this->meta($url);
}
}
?>
<?php
/**
* Index page named index.php or default.php (Whatever your host accepts)
* This is the code that contacts the classes and makes then work.
*/
require_once('class.getPageName.php');
require_once('class.getMetaKeywords.php');
$curpage = new getPageName();
$pagename = $curpage->getName();
$metakeywords = new getMetaKeywords();
$keywords = $metakeywords->getKeywords($pagename);
echo $keywords; // This will echo out your keywords. Simply put it in something like
// echo "<meta name=\"keywords\" content=\"$keywords\">";
?>
-
\$\begingroup\$ Hi welcome to Code Review! We would need a more information on what your application is doing. This will help in reviewing your code. \$\endgroup\$Marc-Andre– Marc-Andre2014年03月02日 22:57:40 +00:00Commented Mar 2, 2014 at 22:57
-
\$\begingroup\$ @Marc-Andre: All I am doing is taking a for loop and sending it to a View. In the view it displays 0 - 100 in the drop down menu. That all works. I am merely trying to teach myself about encapsulation that I hear so much about and I am told I should be doing but nobody yet has been able to tell me how to do it without being overly complicated about it. I started reading tutorials and wrote this simple example. I am merely wondering if this is an example of encapsulation so I know whether or not I am on the correct track. \$\endgroup\$scrfix– scrfix2014年03月02日 23:24:57 +00:00Commented Mar 2, 2014 at 23:24
-
\$\begingroup\$ Nobody knows? This is the third place I have asked. I have everyone telling me that I should do it but does nobody else practice it? I asked on regular stack overflow and they told me to put the question here. I don't need assistance with getting function to work. I merely need to know whether or not what I have is a good example of encapsulation. \$\endgroup\$scrfix– scrfix2014年03月03日 00:08:29 +00:00Commented Mar 3, 2014 at 0:08
-
3\$\begingroup\$ Wait a bit your question is still young and a lot of regular users are not online. \$\endgroup\$Marc-Andre– Marc-Andre2014年03月03日 00:09:39 +00:00Commented Mar 3, 2014 at 0:09
-
\$\begingroup\$ Well its been 3 days. My guess is that nobody actually knows whether these are examples of encapsulation. \$\endgroup\$scrfix– scrfix2014年03月06日 13:48:22 +00:00Commented Mar 6, 2014 at 13:48
1 Answer 1
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.
-
\$\begingroup\$ I totally agree with you! The problem with making code to show something is it's will normally be over complicated for the problem "solved" by the code. \$\endgroup\$Marc-Andre– Marc-Andre2014年06月18日 15:06:01 +00:00Commented Jun 18, 2014 at 15:06
-
\$\begingroup\$ My apologies. I never received an email stating that this was responded to. I had given up on it being responded to. Thanks for the answer. Makes sense. \$\endgroup\$scrfix– scrfix2014年06月30日 02:11:18 +00:00Commented Jun 30, 2014 at 2:11
-
\$\begingroup\$ @scrfix maybe you didnt check back often. Sometimes I dont get emails also, so I check manually. \$\endgroup\$azngunit81– azngunit812014年06月30日 04:54:57 +00:00Commented Jun 30, 2014 at 4:54