douyong1885 2018-12-05 16:32
浏览 33
已采纳

在cakephp中使用PHP Gender扩展

I've got the Gender guessing extension of php working on my localhost. http://php.net/manual/en/gender.example.admin.php

Following the instructions in a non-cakephp project all I have to do is:

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

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

And that produces the correct results. However if I put the above code in a function in cakephp I get an error:

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

I think it's something to do with using namespaces but I'm not really sure how that all works (Have been and will continue to google). Can anyone shed any light on how I can use this Gender extension in cakephp?

EDIT Here is the error logs:

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}

And this is the exact code I'm using in my dashboard function:

$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
", $name, $data['country']);
    break;
    case Gender::IS_MOSTLY_FEMALE:
        printf("The name %s is mostly female in %s
", $name, $data['country']);
    break;
    case Gender::IS_MALE:
        printf("The name %s is male in %s
", $name, $data['country']);
    break;
    case Gender::IS_MOSTLY_MALE:
        printf("The name %s is mostly male in %s
", $name, $data['country']);
    break;
    case Gender::IS_UNISEX_NAME:
        printf("The name %s is unisex in %s
", $name, $data['country']);
    break;
    case Gender::IS_A_COUPLE:
        printf("The name %s is both male and female in %s
", $name, $data['country']);
    break;
    case Gender::NAME_NOT_FOUND:
        printf("The name %s was not found for %s
", $name, $data['country']);
    break;
    case Gender::ERROR_IN_NAME:
        echo "There is an error in the given name!
";
    break;
    default:
        echo "An error occurred!
";
    break;
}
  • 写回答

1条回答 默认 最新

  • dqdl6469 2018-12-05 16:46
    关注

    Without your CakePHP code I can only guess but yes, most likely the namespace creates the problem. CakePHP code has its own namespace and you cannot nest namespaces. Try this code instead:

    $gender = new \Gender\Gender();
    $name = "Milene";
    $country = \Gender\Gender::FRANCE;
    
    $result = $gender->get($name, $country);
    $data = $gender->country($country);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵