我只想初始化 \AsyncMysqlConnectionResult $connec;
对象
<?hh
namespace Connection;
require_once("ini.php");
/**
* Class for execute and fetch query
*/
class MyQuery
{
/**
* if connection isn't valid recreate pool
*/
private ?\AsyncMysqlConnectionPool $pool;
/**
* \AsyncMysqlConnection object, store $conn
*/
private \AsyncMysqlConnection $connec;
/**
* \AsyncMysqlQueryReturn object, store return query
*/
private \AsyncMysqlQueryResult $result;
/**
* check if $conn object isValid(), if not release
* connection
*/
public function __construct(\AsyncMysqlConnection $conn)
{
if ($conn->isValid() == false)
{
$this->pool = new MyPool();
$this->connec = $this->pool->connect();
}
else
$this->connec = $conn;
$this->result = object;
}
/**
* escape query and execute it
*/
public async function query(string $query): Awaitable<\AsyncMysqlQueryResult>
{
$query = $this->connec->escapeString($query);
echo "Query escape\n";
/* Try to execute the query, if fail display error */
try
{
$this->result = await $this->connec->query($query);
//log request with ini
}
catch (Exception $e)
{
echo "Couldn't execute the request, error with message :<br>";
var_dump($e->getMessage());
//log request with fail
}
echo "Query done succefully\n";
return $this->result;
}
/**
* escape Map array and execute the request
*/
public async function queryf(HH\FormatString<HH\SQLFormatter> $query, array<string> $params): Awaitable<\AsyncMysqlQueryResult>
{
$i = 0;
while ($params[$i++])
$params[$i] = $this->connec->escapeString($params[$i]);
/* Try to execute the query, if fail display error */
try
{
$result = await $this->connec->queryf($query, implode(', ', $params));
//log request with ini
}
catch (Exception $e)
{
echo "Couldn't execute the request, error with message :<br>";
var_dump($e->getMessage());
//log request with fail
}
echo "Query done succefully\n";
return $this->result;
}
}
newtype AsyncMysqlConnectionResult = object;
newtype FormatString<T> = string;
async function simple_query(\AsyncMysqlConnection $conn): Awaitable<Vector>
{
$connec = new MyQuery($conn);
$ret = await $connec->query('SELECT * FROM users');
return $ret->vectorRows();
}
function run_query(\AsyncMysqlConnection $conn): void
{
$r = \HH\Asio\join(simple_query($conn));
var_dump($r);
}
run_query($conn);
为了拥有这个对象,我使用 https://docs.hhvm.com/hack/reference/class/AsyncMysqlConnectionPool/connect/类和 connect() 方法具有此:\AsyncMysqlConnectionResult $connec 对象。
我找不到初始化此变量类型的方法,我尝试过
创建新类型 AsyncMysqlConnectionResult = object 但文件检查器返回我:
未绑定(bind)名称:Connection\object(对象类型)
最佳答案
为什么您的查询方法不简单地返回结果,而不是将结果存储在类上。例如:
public async function query(string $query): Awaitable<\AsyncMysqlQueryResult>
{
$query = $this->connec->escapeString($query);
return await $this->connec->query($query);
}
或者,如果您确实希望将结果存储在类中,请将其设置为可为空。这将允许您不在构造函数中设置它,而仅在进行查询后设置它。
private ?\AsyncMysqlQueryResult $result;
关于mysql - 无法初始化\AsyncMysqlQueryResult 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39020813/