In my Classnames and my methods I have named them CssBox and DisplayBox. Just want to get the right syntax here it it a general rule of each word in a class or method being an uppercase?
My second question is that I get the following outputted from the code with no classnames returned and I can’t figure out why:
<div class=''></div>
<div class=''></div>
I understand from what I have heard about OOP that it’s best not to echo anything from within the class itself and you should let the class return values instead and that the database class should only be concerned with retrieving the data. So with the above real world example how could I do that as I see no way I could return values without duplicating code when my instance is created each time?
If you don’t declare them as private then they will be automatically created as public. That means that there is a potential for code elsewhere to update their values and break the processing.
It’s (extremely) good practice to declare them beforehand, and although you can get away with not declaring them, you might implode the space-time continuum if you don’t.
Although, in some situations not declaring them will cause warnings/ errors (e.g. static members, but that’s for another day )
As for the public/ protected/ private bit, that has to do with scope. If you’re not accessing the variables from outside your class/ object, you can safely declare them as private. It just means only the current class has access to them.
If you extend your class, and the extending/ extended class needs to access them, you should declare them as protected (extending classes is something you’ll encounter soon enough if you continue your OOP excercises).
If you need to access them from outside (e.g. $box->boxstyle), then they should be public
A rule of thumb that works quite well for me is:
If you’re not sure what it should be, declare it private. You can always change it later if you need to access a variable from elsewhere.
Why are these needed here as the system seems to run without the need for these as being declared, or is this just good practice for every value you declare to be made public/private/protected?
So for example this runs fine without the need for the above:
Class CssBox {
function displayBox($identifier,$boxstyle) {
$this->identifier = $identifier;
$this->boxstyle = $boxstyle;
return "<div id ='".$this->identifier."' class='".$this->boxstyle."'></div>\
";
}
}
$box = new CssBox();
echo $box->DisplayBox("boxname","box_long");
Thanks again, sorry to be a pain but just want to get this right in my head!
How you name your classes and methods is always a personal choice. I generally use stuff like:
class ClientPropertyManager {
public function isLoggedIn() {}
public function logIn() {}
}
Class names I start with Capital letters and method names with lowercase letters.
But your best bet is to choose a naming system you feel comfortable with
Now for your other questions, see an amended version of your code:
<?php
class CssBox {
private $boxstyle;
public function displayBox($boxstyle) {
$this->boxstyle = $boxstyle;
return "<div class='".$this->boxstyle."'></div>\
";
}
}
$box = new CssBox();
echo $box->DisplayBox("box_long");
?>
First of all, in your echo statement you used $this->boxstyle, but that variable doesn’t exist. In my example, I first assign $boxstyle to $this->boxstyle. Secondly, I return the value, and then echo it in the main flow of the program.
Now you could add other functions, and they could also access $this->boxstyle. Check this somewhat expanded example:
<?php
class CssBox {
private $boxstyle;
private $html;
public function __construct($boxstyle='') {
$this->boxstyle = $boxstyle;
}
public function addInnerHTML($html='') {
$this->html = $html;
}
public function displayBox() {
$output = '<div';
if($this->boxstyle!='') {
$output .= ' class="' . $this->boxstyle . '"';
}
$output .= '>' . $html . '</div>';
return $output;
}
}
$box = new CssBox("box_long");
$box->addInnerHTML('Hello World!');
echo $box->displayBox();
?>