在类中调用函数

时间:2010-05-13 03:28:10

标签: php class function this

我在调用类中的特定函数时遇到问题。打电话:

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

,类代码是:

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;
}
}

当我运行应用程序时 - 我收到以下消息:

  

致命错误:调用未定义   函数validateCreditCard()in   C:\ XAMPP \ htdocs中\ validation.php   在第339行

任何想法?

2 个答案:

答案 0 :(得分:2)

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();

调用方法statically或作为实例方法不一定可以互换,请注意。顺便提一句,basics of OOP就完全可以了。

答案 1 :(得分:0)

包含使用Switch-Case的函数的类是否继承了CreditCardValidationSolution类.... ??

我的猜测是你试图在没有继承它的情况下调用课外的函数....也许你只是错过了它....

阅读评论后编辑:    你需要的是“继承”

看看以下链接.....

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

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

希望这会有所帮助......