在cakephp中使用PHP Gender扩展

时间:2018-12-05 16:32:27

标签: php cakephp

我在localhost上运行了php的Gender猜测扩展。 http://php.net/manual/en/gender.example.admin.php

按照非cakephp项目中的说明,我要做的就是:

namespace Gender;
$gender = new Gender;
$name = "Milene";
$country = Gender::FRANCE;

$result = $gender->get($name, $country);
$data = $gender->country($country);

这将产生正确的结果。但是,如果我将上面的代码放在cakephp中的函数中,则会出现错误:

syntax error, unexpected 'Gender' (T_STRING), expecting \\ (T_NS_SEPARATOR)

我认为这与使用名称空间有关,但我不确定所有的工作方式(曾经并将继续在Google上使用)。谁能阐明我如何在cakephp中使用此Gender扩展程序?

编辑 这是错误日志:

Stack Trace:
#0 [internal function]: OwnerAccountsController->dashboard()
#1 /var/www/html/rrv3/lib/Cake/Controller/Controller.php(491): ReflectionMethod->invokeArgs(Object(OwnerAccountsController), Array)
#2 /var/www/html/rrv3/lib/Cake/Routing/Dispatcher.php(193): Controller->invokeAction(Object(CakeRequest))
#3 /var/www/html/rrv3/lib/Cake/Routing/Dispatcher.php(167): Dispatcher->_invoke(Object(OwnerAccountsController), Object(CakeRequest))
#4 /var/www/html/rrv3/app/webroot/index.php(110): Dispatcher->dispatch(Object(CakeRequest), Object(CakeResponse))
#5 {main}

是我在仪表板功能中使用的确切代码:

$gender = new \Gender\Gender();
$name = "Milene";
$country = \Gender\Gender::FRANCE;
$result = $gender->get($name, $country);
$data = $gender->country($country);

switch($result) {
    case Gender::IS_FEMALE:
        printf("The name %s is female in %s\n", $name, $data['country']);
    break;
    case Gender::IS_MOSTLY_FEMALE:
        printf("The name %s is mostly female in %s\n", $name, $data['country']);
    break;
    case Gender::IS_MALE:
        printf("The name %s is male in %s\n", $name, $data['country']);
    break;
    case Gender::IS_MOSTLY_MALE:
        printf("The name %s is mostly male in %s\n", $name, $data['country']);
    break;
    case Gender::IS_UNISEX_NAME:
        printf("The name %s is unisex in %s\n", $name, $data['country']);
    break;
    case Gender::IS_A_COUPLE:
        printf("The name %s is both male and female in %s\n", $name, $data['country']);
    break;
    case Gender::NAME_NOT_FOUND:
        printf("The name %s was not found for %s\n", $name, $data['country']);
    break;
    case Gender::ERROR_IN_NAME:
        echo "There is an error in the given name!\n";
    break;
    default:
        echo "An error occurred!\n";
    break;
}

1 个答案:

答案 0 :(得分:0)

没有您的CakePHP代码,我只能猜测,但是可以,很可能是名称空间引起了问题。 CakePHP代码具有自己的名称空间,您不能嵌套名称空间。请尝试以下代码:

$gender = new \Gender\Gender();
$name = "Milene";
$country = \Gender\Gender::FRANCE;

$result = $gender->get($name, $country);
$data = $gender->country($country);