繁体   English   中英

在类中调用函数

[英]Calling a function within a class

我在调用类中的特定功能时遇到麻烦。 拨打电话:

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

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

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

有任何想法吗?

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

注意, 静态地或作为实例方法调用的方法不一定可以互换。 顺便说一下,OOP基础知识已经很好地覆盖了所有内容。

包含您在其中使用Switch-Case的函数的类是否继承CreditCardValidationSolution类。

我的猜测是您试图在类外部调用函数而不继承它。...也许您只是错过了它。

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

看一下以下链接.....

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

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

希望这可以帮助....

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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