[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Undefined Function in PHP?

Posted on 2011-10-06
9
Medium Priority
?
321 Views
Last Modified: 2012-05-12
I have a class the has a few methods (see code) but when I try to run the script it says "Fatal error: Call to undefined function displayReps3()..."

However, the function is defined directly above where it says it's not...

Note:

// this is the line where PHP says the function does not exist...
$sql = displayReps3($state, $country, $industry, $product, true);
// if no reps are found, displays worldwide contact, product manager or employees
	function displayReps3($territory, $continent, $market, $product, $employee) {
		// include reps function for brevity of code
		require "components/com_catalog/includes/pullReps.php";
		// find employee reps
		if (($repList = pullReps($territory, $continent, $market, $product, true)) != NULL) {
			die(var_dump($repList[1]));
			return var_dump($repList[1]);
		// if no reps are found, display product manager
		} else {
			if (($repList = pullReps($territory, $continent, $market, $product, $employee, true)) != NULL) {
				return var_dump($repList[1]);
			// if no reps are found, display worldwide contact
			} else {
				$repList = pullReps($territory, $continent, $market, $product, $employee, false, true);
				return var_dump($repList[1]);
			}
		}
	}
	
	function getRepByGeo() {
	
		require "components/com_catalog/includes/QuoteCountries.php";

		$country = JRequest::getVar('country');
		$product = JRequest::getVar('catalog'); 	// product
		$industry = JRequest::getVar('application'); 	// industry
		$state = JRequest::getVar('state');
						
		// #######  Determine Territory Sql to Pull Reps	
		
			//if (function_exists(displayReps3)) { die('displayReps3 exists'); } else { die('no'); }
			$sql = displayReps3($state, $country, $industry, $product, true);

Open in new window

0
Comment
Question by:EMB01
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 13

Expert Comment

by:haloexpertsexchange
ID: 36925210
the problem is that you are calling that function from with in another function and for the getRepByGeo function the displayReps3 function does not exist.
0
 
LVL 13

Assisted Solution

by:haloexpertsexchange
haloexpertsexchange earned 200 total points
ID: 36925224
functions can only see what information is passed to them or global information like sessions and cookies or global variables. so unless you define the displayReps3 function again within the getRepByGeo, it will not know what you are looking for.
0
 
LVL 10

Accepted Solution

by:
Derokorian earned 1600 total points
ID: 36925273
This isn't true.. A function can always see other previously defined functions. However the problem is that you say this is part of a class, so you want to be accessing this function using

$this->displayReps3();
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 13

Expert Comment

by:haloexpertsexchange
ID: 36925295
you are right, I guess I had not thought it all the way through, if it is a part of a class you are try in to access it incorrectly.
0
 
LVL 34

Assisted Solution

by:Slick812
Slick812 earned 200 total points
ID: 36925337
greetings EMB01, , , not sure about what you have coded, you do include the two functions that are giving you trouble, however, in your question you say "I have a class the has a few methods"

if these functions are in a Class, then you have to call a function with the
$this->
before the method name -
$sql = $this->displayReps3($state, $country, $industry, $product, true);

There are many PHP class tutorials, and much information in the in the online PHP manual about PHP CLasses, have you looked at these?
0
 
LVL 34

Expert Comment

by:Slick812
ID: 36925350
sorry Derokorian, did not mean to step on you, you posted while I was typing.
0
 
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 36925363
@halo, I call functions from other functions without problems.  What I can't get, without passing them, is variables.  Perhaps I don't understand what you are trying to say.

@EMB, I don't see the problem.  You said you have created a class but I don't see any objects.

Could you post enough of the code that I can run it (preferably from the command line)?  I often do a better job at this if I can run the program myself.
0
 
LVL 14

Author Closing Comment

by:EMB01
ID: 36925377
Actually, all solutions worked, even putting the first function inside of the second function.  But, the best solution was Derokorian's.
0
 
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 36925404
Wow, a lot of stepping on each other.

I think Derokorian might be right.  I haven't yet had a reason to call a class function from inside a class function.  I thought about that answer but figured I'd figure it out if I had the code to play with.  Probably don't need it now.

IMHO, in the future, it would help to post more of the problem code even if you leave things out.

class Foo
{
    // bunch of lines omitted
   
    function getFoo()  { .... }

    function setFoo ( $foo )  { .... }
}


Don't need everything.  But complete functions would help.  Just my 2 cents.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to implement server side field validation and display customized error messages to the client.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month20 days, 3 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question