View Full Version : Resolved OOP method
ggalan
11-28-2011, 09:38 PM
i have a function here that works well
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
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?
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;
}
}
ggalan
11-28-2011, 09:57 PM
this seems to work
require_once("CountArrayValues.php");
$count_array = new CountArrayValues();
then
$count_array->count_repeat_values($itm, $arr);
ggalan
11-28-2011, 10:16 PM
but i have a class within a class
normally where would you put the include statement? like this on top?
OtherClass
{
require_once("CountArrayValues.php");
function myMethod(){
}
}
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
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 (http://us.php.net/manual/en/language.oop5.static.php)) or put them in their own namespace (http://us.php.net/manual/en/language.namespaces.php).
ggalan
11-29-2011, 03:06 AM
i see, so you recommend placing 'count_repeat_values' above 'SomeClass' in the same file? should i name it static function?
static function count_repeat_values( $needle,$haystack ){ /* ... */ }
ggalan
12-06-2011, 02:13 AM
so i changed this to static function but i seem to be having a problem accessing it.
<?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
<?php
class SomeClass
{
static function count_repeat_values( $needle,$haystack ){ /* ... */ }
public function someFunction( $array ){
$item = 'key';
$num = SomeClass::count_repeat_values( $item,$array );
}
}
In your first example:
<?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
// 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 );
}
}
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.