当我把它放在函数中时,我的PDO对象会发生什么?

时间:2013-11-07 16:38:11

标签: php object

我有一个名为$ db的对象,它在函数内部输出其他东西:

<?php
$dsn = "pgsql:"
    . "host=ec2-54-217-239-27.eu-west-1.compute.amazonaws.com;"
    . "dbname=dat08vv8oqt1j1;"
    . "user=secret :);"
    . "port=5432;"
    . "sslmode=require;"
    . "password=secret :)";

$db = new PDO($dsn);
$sql = "create table parent_children (
    parent_path varchar,
    child_path varchar,
    primary key(parent_path,child_path),
    foreign key (parent_path) references nodes (path),
    foreign key (child_path) references nodes (path)
)";
/*$sq = $db ->query($sql);*/

print_r($db);
myqueries($sql, $db);

function myqueries($myobject, $sql){
    print_r($myobject);
    die();
};

第一次输出

PDO Object ( )

下一次:

create table parent_children ( parent_path varchar, child_path varchar, primary key(parent_path,child_path), foreign key (parent_path) references nodes (path), foreign key (child_path) references nodes (path) )

问题是这在函数内部不起作用:

$sq = $myobject ->query($sql);

但它在它之外工作。

1 个答案:

答案 0 :(得分:0)

查看函数args。尝试:

myqueries($db, $sql);

来自raina77ow的评论:

function myqueries(PDO $myobject, $sql) {}

如果$ myobject不是PDO类型,则会抛出错误。