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 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?