Coding confusion in OOP

Discussion in 'PHP' started by Silver89, Mar 29, 2011.

  1. #1
    I'm starting to change my coding practise from Procedural to OOP but am coming across a few small things that I'm not too sure about..

    If you have a class of tools that you want to use within other classes so:

    
    class Tools {
    	
    	public function swearFilter($comment) {
    		
    		$swear = array("gay");
    		$comment = str_replace($swear, "[oops] ", $comment);
    		return $comment;
    	}
    }
    
    PHP:
    and I want to use it within another class then how would I do that?

    Is the best way to do as follows?

    
    class Test {
    	
    	public function displayComment($commentBody) {
    		
    		$globalFunctions = new Tools();
    		$commentBody = $globalFunctions->swearFilter($commentBody);
    		return $commentBody;
    	}
    }
    
    PHP:
     
    Silver89, Mar 29, 2011 IP
  2. heksagonas

    heksagonas Member

    Messages:
    2
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #2
    You don't need to create object in this case, you can call the method directly:

    
    class Test {
    	
    	public function displayComment($commentBody) {
    		
    		$commentBody = Tools::swearFilter($commentBody);
    		return $commentBody;
    	}
    }
    
    PHP:
    Yet both these examples are not the proper way to do OOP. In fact your class Test should inherit class Tools and then call the method using $this->swearFilter().
     
    heksagonas, Mar 29, 2011 IP
    Silver89 likes this.
  3. jazzcho

    jazzcho Peon

    Messages:
    326
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    You are on the right track. :)
    As heksagonas said, you can call the function statically without creating an instance of the Tools class.

    @heksagonas:
    He probably wants to use it on different classes which may not be suited to be extending the Tools class. In that case, I wouldn't do inheritance.
     
    jazzcho, Mar 29, 2011 IP
    Silver89 likes this.
  4. Silver89

    Silver89 Notable Member

    Messages:
    2,243
    Likes Received:
    72
    Best Answers:
    0
    Trophy Points:
    205
    #4
    Ah yes I wasn't sure about the inheritance part, that helps!

    If you have functions you want to use sitewide then how would you go about that?

    Is it best to have like a top class then extend all the others off of that to inherit things such as logged in user checking?
     
    Silver89, Mar 29, 2011 IP
  5. jazzcho

    jazzcho Peon

    Messages:
    326
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #5
    A)
    If you have a lot of functions, you can call them directly from their classes.

    BUT!

    OOP is not about wrapping the functions into a class {}. It 's about designing your program in the OO philosophy. Try to read a bit about that, it will set you on a good track.

    B)
    PHP does not have multiple inheritance support, so what happens it you need to extend from many classes?

    Google a bit about "Dependency Injection", "Invertion of Control" and mixins and you will get inspired.

    Personally, I use a lot DI and IoC.
     
    jazzcho, Mar 29, 2011 IP
  6. heksagonas

    heksagonas Member

    Messages:
    2
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #6
    That's exactly what I wanted to say. If your classes are just a collections of static methods you are missing the point of OOP. In this case you should probably leave them as functions.
     
    heksagonas, Mar 30, 2011 IP
  7. Silver89

    Silver89 Notable Member

    Messages:
    2,243
    Likes Received:
    72
    Best Answers:
    0
    Trophy Points:
    205
    #7
    Another question..

    When using functions is it better to initiate the values you want to use within as $this-> or sending them into the function as parameters?
     
    Silver89, Mar 30, 2011 IP