简体   繁体   中英

Calling a function within a class

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?

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.

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....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM