views:

91

answers:

2

I am having trouble calling a specific function within a class. The call is made:

   case "Mod10":        
        if (!validateCreditCard($fields[$field_name]))
      $errors[] = $error_message;
    break;

and the class code is:

class CreditCardValidationSolution {

    var $CCVSNumber = '';
    var $CCVSNumberLeft = '';
    var $CCVSNumberRight = '';
    var $CCVSType = '';
    var $CCVSError = '';

function validateCreditCard($Number) {

        $this->CCVSNumber      = '';
        $this->CCVSNumberLeft  = '';
        $this->CCVSNumberRight = '';
        $this->CCVSType        = '';
        $this->CCVSError       = '';

        // Catch malformed input.

        if (empty($Number) || !is_string($Number)) {
            $this->CCVSError = $CCVSErrNumberString;
            return FALSE;
        }

        // Ensure number doesn't overrun.
        $Number = substr($Number, 0, 20);

        // Remove non-numeric characters.
        $this->CCVSNumber = preg_replace('/[^0-9]/', '', $Number);

        // Set up variables.
        $this->CCVSNumberLeft  = substr($this->CCVSNumber, 0, 4);
        $this->CCVSNumberRight = substr($this->CCVSNumber, -4);
        $NumberLength          = strlen($this->CCVSNumber);
        $DoChecksum            = 'Y';

        // Mod10 checksum process...
        if ($DoChecksum == 'Y') {

            $Checksum = 0;

            // Add even digits in even length strings or odd digits in odd length strings.
            for ($Location = 1 - ($NumberLength % 2); $Location < $NumberLength; $Location += 2) {
                $Checksum += substr($this->CCVSNumber, $Location, 1);
            }

            // Analyze odd digits in even length strings or even digits in odd length strings.
            for ($Location = ($NumberLength % 2); $Location < $NumberLength; $Location += 2) {
                $Digit = substr($this->CCVSNumber, $Location, 1) * 2;
                if ($Digit < 10) {
                    $Checksum += $Digit;
                } else {
                    $Checksum += $Digit - 9;
                }
            }

            // Checksums not divisible by 10 are bad.
            if ($Checksum % 10 != 0) {
                $this->CCVSError = $CCVSErrChecksum;
                return FALSE;
            }
        }

        return TRUE;
}
}

When I run the application - I get the following message:

Fatal error: Call to undefined function validateCreditCard() in C:\xampp\htdocs\validation.php on line 339

any ideas?

+1  A: 
class Foo {

    // How may I be called?
    function bar() {
    }

    function baz() {
        // Use $this-> to call methods within the same instance
        $this->bar();
    }

    function eek() {
        // Use self:: to call a function within the same class statically
        self::bar();
    }

}

// Use [class]:: to call a class function statically
Foo::bar();

// Use [object]-> to call methods of objects
$fooInstance = new Foo();
$fooInstance->bar();

Calling methods statically or as an instance method is not necessarily interchangeable, beware. That's all pretty well covered in the basics of OOP by the way.

deceze
@deceze - I saw this example elsewhere but not sure it applies to what I'm doing. Maybe it does and I'm misreading but I am basically trying to call a function and have it run through validations. Unfortunately, the function I found is laid inside of a class (and laden with $this variables) so I keep running into issues. I have another CASE and FUNCTION working in the exact same method with the exception there is no class and $this var
JM4
@JM4 If the function is part of a class, you'll need to call it as `[class]::` or `[object]->`, the functions are *not* part of the global namespace. Whether to call the function statically (`::`) or as instance method of an object (`->`) depends on the design of the function. Since it uses `$this`, it *needs* to be called as an instance method (`->`). So you'll have to create a new instance of the class (using `new`). There may be strings attached to instantiating the class, that should be documented or you'll have to read the code carefully.
deceze
@JM4 In more concrete code: `$ccVal = new CreditCardValidationSolution(); $ccVal->validateCreditCard();`
deceze
A: 

Does the class containing the function within which you use the Switch-Case inherit the CreditCardValidationSolution Class....??

My guess would be that you are trying to call the function outside the class without inheriting it.... Maybe you just missed it....

Edit After Reading Comment : What you need here is "Inheritance"

Take a look at the following links.....

http://www.killerphp.com/tutorials/object-oriented-php/php-objects-page-4.php

http://marakana.com/blog/examples/php-inheritance.html

Hope this helps....

SpikETidE
i am not using switch-case at all within the class. I am in the following:Function1() trying to call Function2() and Function2() lives in ClassA but Function1 does notI have not inherited it at all - I am very new to this so honestly I dont even know what that means but I think you are right. I am trying to call a function which resides inside of a class from the outside
JM4