#1
  1. For POny!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    Amsterdam
    Posts
    416
    Rep Power
    115

    Function libraries


    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.
    PHP Code:
    echo $this->AwesomeFunctionOfWorldDomination(); 
    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).
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,113
    Rep Power
    9398
    What kinds of functions? They may belong in the class as static methods or they may belong somewhere else.
  4. #3
  5. For POny!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    Amsterdam
    Posts
    416
    Rep Power
    115
    Originally Posted by requinix
    What kinds of functions? They may belong in the class as static methods or they may belong somewhere else.
    Cheers for responding.
    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:
    PHP Code:
    class User{
        
    // some functions here

    Point is every new user must have valid stuff, by also non-users might require the same check.

    -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.

    PHP Code:
    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"' '';

            
    // Initialize JavaScript field attributes.
            
    $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 '/>';
        }

    So forms are dealt with as entities, with all their features as child classes(extensions).
    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 05:35 PM.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    48
    Rep Power
    3
    Originally Posted by aeternus
    Cheers for responding.
    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:
    PHP Code:
    class User{
        
    // some functions here

    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.

    PHP Code:
    class User{
        
    $myVar 1// a property of User

        
    function myMethod() // a method of User
        
    {
             
    // some lines of code
        
    }

    You may want to also research the difference between public, private and protected methods, as well as static methods.

    Good luck!
    Laterna Studio - Animation, Web Design, Video Production and Print Design

    Psalm 73: 25-26
  8. #5
  9. For POny!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    Amsterdam
    Posts
    416
    Rep Power
    115
    Thanks Piper for your response, I was luckily aware of this terminology.

    Comments on this post

    • piperpam27 agrees : Good--didn't notice the word "method" in your initial post in the first read-through. My bad! =)
  10. #6
  11. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2009
    Posts
    335
    Rep Power
    246
    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.
    Free MP3 Dance Music Downloads

    To err is human; To really balls things up you need Microsoft!

IMN logo majestic logo threadwatch logo seochat tools logo