我很难在类中设置一个公共变量:
function printCaptcha($formId = NULL, $type = NULL, $fieldName = NULL) {
require_once(SITE_ROOT . '/visualCaptcha/inc/visualcaptcha.class.php');
$visualCaptcha = new \visualCaptcha\Captcha($formId, $type, $fieldName);
$visualCaptcha->imageFile = BASE_URL . "image.php";
$visualCaptcha->show();
}$imageFile是我试图设置的那个。其余的都能正常工作。
课内:
public static $imagesPath = 'images/visualcaptcha/';
public static $audiosPath = 'audio/visualcaptcha/';
public static $imageFile = 'image.php';
public static $audioFile = 'audio.php';类的完整代码:https://github.com/emotionLoop/visualCaptcha-PHP/blob/master/inc/visualcaptcha.class.php
发布于 2013-12-24 00:21:17
静态地设置它
\visualCaptcha\Captcha::$imageFile = BASE_URL . "image.php";请参阅http://php.net/manual/language.oop5.static.php#example-194
在PHP5.3中,您还可以使用类实例变量
$visualCaptcha::$imageFile = BASE_URL . "image.php";虽然对我来说,这是令人困惑和丑陋的。
发布于 2013-12-24 00:22:38
静态变量绑定到该类。不是那个物体。
为此,您必须通过静态上下文访问它:
\visualCaptcha\Captcha::$imageFile = BASE_URL . "image.php";(如果您想为此使用"the object“,可以使用get_class()获取对象的类名,然后用以下方式调用它:$classname::$var)
但是,您可能希望了解何时使用静态变量以及何时使用“普通”类和对象成员。
https://stackoverflow.com/questions/20753153
复制相似问题