Results 1 to 7 of 7

Thread: OOP method

  1. #1
    Join Date
    Jan 2008
    Posts
    441
    Thanks
    67
    Thanked 4 Times in 4 Posts

    Default OOP method

    i have a function here that works well
    Code:
    function count_repeat_values($needle, $haystack){
        $x = count($haystack);
        for($i = 0; $i < $x; $i++){
           
            if($haystack[$i] == $needle){
                $needle_array[] = $haystack[$i];
            }
        }
        $number_of_instances = count($needle_array);
        return $number_of_instances;
    }
    then to call it
    Code:
    echo count_repeat_values('a', $array);
    i would like to turn this into a class for reusability and also i dont want to write this everytime i need it.
    how would i go about this?
    ~a newb to OOP

    like this?
    Code:
    class CountArrayValues
    {
    
    	function count_repeat_values($needle, $haystack){
    		$x = count($haystack);
    		for($i = 0; $i < $x; $i++){
    		   
    			if($haystack[$i] == $needle){
    				$needle_array[] = $haystack[$i];
    			}
    		}
    		$number_of_instances = count($needle_array);
    		return $number_of_instances;
    	}
    
    }
    Last edited by ggalan; 11-30-2011 at 03:19 PM.

  2. #2
    Join Date
    Jan 2008
    Posts
    441
    Thanks
    67
    Thanked 4 Times in 4 Posts

    Default

    this seems to work
    Code:
    require_once("CountArrayValues.php");
    $count_array = new CountArrayValues();
    then
    Code:
    $count_array->count_repeat_values($itm, $arr);

  3. #3
    Join Date
    Jan 2008
    Posts
    441
    Thanks
    67
    Thanked 4 Times in 4 Posts

    Default

    but i have a class within a class
    normally where would you put the include statement? like this on top?
    Code:
    OtherClass
    {
      require_once("CountArrayValues.php");
      function myMethod(){
    
      }
    }

  4. #4
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    a self-sufficient function like this doesn't need a class - it's just extra overhead.
    there's nothing wrong with keeping utility functions like this around. simply define them once and use them whenever.
    PHP Code:
    <?php

    function count_repeat_values$needle,$haystack ){ /* ... */ }

    class 
    SomeClass{
         public function 
    someFunction$array ){
              
    $item 'key';
              
    count_repeat_values$item,$array ); 
              
    // works just fine
              // and you didn't have to instantiate a second object,
                 // or worry about scope
         
    }
    }
    if you want to keep the global scope uncluttered, you can use a utility class (if you do, it's best to keep methods like this static) or put them in their own namespace.

  5. #5
    Join Date
    Jan 2008
    Posts
    441
    Thanks
    67
    Thanked 4 Times in 4 Posts

    Default

    i see, so you recommend placing 'count_repeat_values' above 'SomeClass' in the same file? should i name it static function?
    Code:
    static function count_repeat_values( $needle,$haystack ){ /* ... */ }

  6. #6
    Join Date
    Jan 2008
    Posts
    441
    Thanks
    67
    Thanked 4 Times in 4 Posts

    Default

    so i changed this to static function but i seem to be having a problem accessing it.

    Code:
    <?php
    
    static function count_repeat_values( $needle,$haystack ){ /* ... */ }
    
    class SomeClass{
         public function someFunction( $array ){
              $item = 'key';
              $num = count_repeat_values( $item,$array ); 
         }
    }

    NM, forgot the class name


    Code:
    <?php
    class SomeClass
    {
        static function count_repeat_values( $needle,$haystack ){ /* ... */ }
    
         public function someFunction( $array ){
              $item = 'key';
              $num = SomeClass::count_repeat_values( $item,$array ); 
         }
    }

  7. #7
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    In your first example:
    PHP Code:
    <?php

    // incorrect:
    // static function count_repeat_values( $needle,$haystack ){ /* ... */ }
    // correct:
    function count_repeat_values$needle,$haystack ){ /* ... */ }

    class 
    SomeClass{
         public function 
    someFunction$array ){
              
    $item 'key';
              
    $num count_repeat_values$item,$array ); 
              
    // now it should work just fine
         
    }
    }
    in your second example:
    PHP Code:
    <?php

    // this works just fine also.
    // however:
    /*
    Is the function used anywhere else, outside of this class?
    if so, it would be fine to define it in the global scope (as in the example above).
    I think it makes more sense that way.
    It doesn't _have_ to be object-oriented if there's no real reason for it to be.
    */

    class SomeClass
    {
        static function 
    count_repeat_values$needle,$haystack ){ /* ... */ }

         public function 
    someFunction$array ){
              
    $item 'key';
              
    $num SomeClass::count_repeat_values$item,$array ); 
         }
    }

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •