doulu5717 2015-02-01 05:22
浏览 84
已采纳

在Zephir中使用PDO

As part of my experiments with Zephir I am currently trying to use PHP PDO to access a MySQL database. For starters I found that a relatively innocuous

$dbh = new PDO("mysql:host=localhost;dbname=dbn;","user","pwd");

when translated and used in Zephir

var dbh = new PDO("mysql:host=localhost;dbname=dbn;","user","pwd");

had Zephir throwing up an exception

var dbh = new PDO
------------^

which by dint of some searching I resolved - new is a reserved word in Zephir and must be replaced with $new.

var dbh = $new PDO("mysql:host=localhost;dbname=dbn;","user","pwd");

which promptly produced

var dbh = $new PDO(
-----------------^

which I resolved when I realized that I had to explicitly tell Zephir to use the PDO name space

use \PDO;
var dbh = $new \PDO::PDO(

Now, with

var dbh = $new \PDO::PDO("mysql:host=localhost;dbname=dbn","user","pwd");

I get

var dbh = $new \PDO::PDO(...,"user","pwd");
---------------------------------------------^

which makes little sense to me.

From what I can tell Zephir is still too young to be considered for a full port of a working PHP prototype. However, it looks like it is good enough to be used to port some of the more CPU intensive bits of a PHP application but its documentation is lacking. For instance, nowhere does it state in the docs that the right way to use an array is

array myArray;
 let myArray = [1,2,...];

Miss out the first list and the compiler complains about not being able to mutate.

With my current PDO problem there is a clearly something else that is wrong but I have no idea what it might be. I'd much appreciate any help.

  • 写回答

1条回答 默认 最新

  • donglinxia1541 2015-02-16 17:02
    关注

    The var keyword in zephir is for variable declarations. If you assign a "simple" value like a string or an array, it will work.

    The problem with your example is that new PDO(<arguments>) is an expression, where the var operator is not the right choice. So what you want is to assign a value.

    Variables are by default immutable. (see zephir-lang.com)
    That's why you need to use the let operator, which makes them immutable and is able to resolve expressions.

    So you need to use var and let
    like var dbh; let dbh = new \PDO(<arguments>);

    And if you do it that way, it works :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)