Noob Q, using array_filter in class w/ class-wide variable

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
xpt
Forum Newbie
Posts: 23
Joined: Tue Feb 20, 2007 1:00 pm

Noob Q, using array_filter in class w/ class-wide variable

Post by xpt »

Hi,

I have a noob question about OO in PHP.

My ultimate goal is just to use 'array_filter' function in PHP class. Please take a look at the following code:

Code: Select all

class testClass extends baseTestClass
{
    public $canned_answers = 
	array(
	      "Case opened.",
	      "Update case details.",
	      "Case suspended.",
	      "Case closed.",
	      );

    static function containsStr($value)
    {
	$pos = stripos($value, $this->suggested_key);
	return $pos == false ? false : true; 
    }
    
    public function do_test()
    {
	$this->suggested_key = $this->getRequestParameter('comments');
	$this->suggested_answers =
	    array_filter($canned_answers, "testClass::containsStr"); ;
    }
First of all, I want to define a class-wide variable $canned_answers, but I got:

Code: Select all

Notice: Undefined variable: canned_answers.
If I move the variable $canned_answers definition into function do_test, I will still get:

Code: Select all

Warning: array_filter() [function.array-filter]: An error occurred while invoking the filter callback
I guess the containsStr is not static anyway.

So, how can I use 'array_filter' function in PHP class?

thanks.

xpt
Last edited by xpt on Wed Apr 11, 2007 11:43 am, edited 2 times in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1 wrote:2. Use descriptive subjects when you start a new thread. Vague titles such as "Help!", "Why?" are misleading and keep you from receiving an answer to your question.
You're missing "$this->"
xpt
Forum Newbie
Posts: 23
Joined: Tue Feb 20, 2007 1:00 pm

Post by xpt »

feyd wrote: You're missing "$this->"
Like this?

Code: Select all

array_filter($canned_answers, $this->containsStr);

Warning: array_filter() [function.array-filter]: The second argument, '', should be a valid callback



xpt
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

....for $canned_answers.
xpt
Forum Newbie
Posts: 23
Joined: Tue Feb 20, 2007 1:00 pm

Post by xpt »

For

Code: Select all

array_filter($this->$canned_answers, ...
I still get:

Notice: Undefined variable: canned_answers


xpt
xpt
Forum Newbie
Posts: 23
Joined: Tue Feb 20, 2007 1:00 pm

Post by xpt »

just double checked, no spelling errors.
xpt
Forum Newbie
Posts: 23
Joined: Tue Feb 20, 2007 1:00 pm

Post by xpt »

The most important problem is, the second argument to array_filter should be a *name* for valid callback function. but that function can't be static...

So, how can I do that?
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post by Begby »

First off you cannot set the values of an array as part of a class declaration, that needs to be in the constructor.

Secondly its $this->varname not $this->$varname

Thirdly your callback doesn't have to be static, but could work.

Fourthly, do this for your callback -> array_filter($this->canned_answers, array($this, 'containsStr');
Last edited by Begby on Wed Apr 11, 2007 12:34 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The snippet you posted originally has several examples of accessing properties and methods of the class.

Callbacks can most certainly be static. http://php.net/language.pseudo-types
xpt
Forum Newbie
Posts: 23
Joined: Tue Feb 20, 2007 1:00 pm

Post by xpt »

Begby wrote:...Thirdly your callback doesn't have to be static, but could work...
Thanks a lot Begby. Now I've made 4 big forward steps and arrived at the point:

Code: Select all

Fatal error: Using $this when not in object context in
which refers to:

Code: Select all

static function containsStr($value)
    {
	$pos = stripos($value, $this->suggested_key);
	return $pos == false ? false : true; 
    }
Any way to get around this?

thanks

xpt
xpt
Forum Newbie
Posts: 23
Joined: Tue Feb 20, 2007 1:00 pm

Post by xpt »

xpt wrote:Any way to get around this?
Please consider the question comes from a noob of php (but not other programming languages), i.e., the solution is quite easy. In other programming languages -- defining a global variable to hold the value. Though it is not an elegant one, it works and it is efficient.

I just need you to show me how to define a global variable, assign it in class method, and access it in a plain php function; or if you have any other better ideas. Sorry for the noob php question.

please help.
thanks


xpt
Post Reply