December 5th, 2012, 06:05 PM
Hi guys in my quest to master OOP a question kept smacking my head.
The tutorial I'm doing as per usual explains the concept of OOP in the form of entities as part of a certain species. In particular. Class Animal, extended by the class mammals which is extended by class dog, cat, and for the sake of it unicorns.
I have a feeling I understand this hierarchy stuff. As far as I can see it, the benefit of this approach is to share (in a layered way) code in an efficient way between entities.
I have a feeling sometimes functions (so not entities) could be used also outside of the scope of the class. Now this is where I get a bit hesitant. I would think that if I reuse code, that is not in particular bound to a class, it should put it in a separate library (i think we can call them "super" public functions than). But when I put it in a separate library I am not using them as a "method" like.
Now the question (lol sorry for that)
I am almost certain that if I would put functions in a separate library I would not make them classes right? I can't think of any entity (except for unicorns) as a function. Not to mention I would than have to make an instance of them, which seems weird to me. So in a nutshell, a function library should not be build up with classes, but as functions that probably share some common things (like form validator functions).
December 5th, 2012, 06:15 PM
What kinds of functions? They may belong in the class as static methods or they may belong somewhere else.
December 5th, 2012, 06:21 PM
Cheers for responding.
Originally Posted by requinix
I mean for instance functions to validate user input that are required on register but are also used in other situations (validate: firstname, lastname, phone, etc)
I could put these within a class like:
Point is every new user must have valid stuff, by also non-users might require the same check.
// some functions here
-edit: hmm I am looking through joomla code and the libraries pretty much are all classes. even for forms.
-edit2: Ah I think I see what is going on. The form is the Top entity, and below in hierarchy are the parts of the form like checkboxes.
So forms are dealt with as entities, with all their features as child classes(extensions).
class JFormFieldCheckbox extends JFormField
* The form field type.
* @var string
* @since 11.1
public $type = 'Checkbox';
* Method to get the field input markup.
* The checked element sets the field to selected.
* @return string The field input markup.
* @since 11.1
protected function getInput()
// Initialize some field attributes.
$class = $this->element['class'] ? ' class="' . (string) $this->element['class'] . '"' : '';
$disabled = ((string) $this->element['disabled'] == 'true') ? ' disabled="disabled"' : '';
$checked = ((string) $this->element['value'] == $this->value) ? ' checked="checked"' : '';
$onclick = $this->element['onclick'] ? ' onclick="' . (string) $this->element['onclick'] . '"' : '';
return '<input type="checkbox" name="' . $this->name . '" id="' . $this->id . '"' . ' value="'
. htmlspecialchars((string) $this->element['value'], ENT_COMPAT, 'UTF-8') . '"' . $class . $checked . $disabled . $onclick . '/>';
hmm, I think I need to read a bit morebut i have a feeling I am on the right track
Last edited by aeternus; December 5th, 2012 at 06:35 PM.
December 6th, 2012, 12:03 AM
Just for the sake of accuracy (and perhaps better results in your Google searches) I'd like to point out that functions inside a class are actually called "methods". On the same token, variables of a class are called properties. See below.
Originally Posted by aeternus
You may want to also research the difference between public, private and protected methods, as well as static methods.
$myVar = 1; // a property of User
function myMethod() // a method of User
// some lines of code
- Animation, Web Design, Video Production and Print Design
Psalm 73: 25-26
December 6th, 2012, 10:28 AM
Thanks Piper for your response, I was luckily aware of this terminology.
Comments on this post
December 6th, 2012, 11:15 AM
variables of an object are also known as attributes, especially if you have provided getter and setter (Accessor) methods to them.
dependent of the access modifier methods are also know as the message protocol. (public methods)
I don't know if PHP has functions as well as methods, but in Perl (which PHP was based on); if it is meant to be a function it doesn't belong in a class and should not be a method.
methods are called against an object, functions are just ran as standalone code.
Unless PHP is like Java where there are no functions and you are required to have static classes that aren't instantiated to call a 'Class Method' then you should be using functions not methods.