Background
我正在为 TeraWURFL 编写一个新的数据库连接器文件,该文件将使用 PDO 扩展名。有关此内容的更多详细信息,请查看
详
我添加了一个新的数据库连接器 - TeraWurflDatabase_MySQL5_Mine.php
并在配置文件中定义了相同的连接器 - TeraWurflConfig.php
(已经有许多连接器文件随 TeraWURFL 一起提供,配置文件中的值决定使用哪一个) -
public static $DB_CONNECTOR = "MySQL5_Mine";
然后,我编写了一个测试脚本来对新TeraWurflDatabase_MySQL5_Mine.php
类文件的所有函数进行单元测试,以确保它们在更改后返回相同的值。我在测试脚本中遇到问题。
一切正常,但是TeraWurflDatabase_MySQL5_Mine.php
内部一个名为rebuildCacheTable()
的功能出了问题。当我从测试脚本调用此函数时,控件不会在该函数内的 return 语句之后返回到我的测试脚本。 -
测试脚本中的代码 test_connector.php
-
/*file inclusiong start*/
require_once '/var/cake_1.2.0.6311-beta/app/webroot/Tera-Wurfl_PDO/TeraWurfl.php';
require_once '/var/cake_1.2.0.6311-beta/app/webroot/Tera-Wurfl_PDO/TeraWurflConfig.php';
require_once '/var/cake_1.2.0.6311-beta/app/webroot/Tera-Wurfl_PDO/DatabaseConnectors/TeraWurflDatabase.php'; //this file is same on both setups
require_once '/var/cake_1.2.0.6311-beta/app/webroot/Tera-Wurfl_PDO/TeraWurflLoader.php';
require_once '/var/cake_1.2.0.6311-beta/app/webroot/Tera-Wurfl_PDO/UserAgentFactory.php';
require_once '/var/cake_1.2.0.6311-beta/app/webroot/Tera-Wurfl_PDO/UserAgentUtils.php';
require_once '/var/cake_1.2.0.6311-beta/app/webroot/Tera-Wurfl_PDO/WurflConstants.php';
require_once '/var/cake_1.2.0.6311-beta/app/webroot/Tera-Wurfl_PDO/WurflSupport.php';
require_once '/var/cake_1.2.0.6311-beta/app/webroot/Tera-Wurfl_PDO/UserAgentMatchers/UserAgentMatcher.php';
require_once '/var/cake_1.2.0.6311-beta/app/webroot/Tera-Wurfl_PDO/DatabaseConnectors/TeraWurflDatabase_MySQL5_Mine.php';
/*file inclusiong end*/
$wurflDbPdoObj = new TeraWurflDatabase_MySQL5_Mine();
$wurflDbPdoObj->connect();
$dbObj = $wurflDbPdoObj;
echo "before call";
var_dump($dbObj->rebuildCacheTable());
echo "after call"; //this does not execute if new TeraWurfl() object is initiated below
和TeraWurflDatabase_MySQL5_Mine
里面的代码——
class TeraWurflDatabase_MySQL5_Mine extends TeraWurflDatabase{
...
...
public function rebuildCacheTable(){
$rebuilder = new TeraWurfl(); //if this object is initiated, return does not happen properly
return true; //this does not return properly.
}
}
到目前为止我的调试
我验证了$rebuilder = new TeraWurfl();
启动的对象是正确的对象。
我可以缩小到这个范围,如果TeraWurflDatabase_MySQL5_Mine
内部的线$rebuilder = new TeraWurfl();
被移除,则返回工作正常。
另外,如果我调用现有连接器类的相同函数TeraWurflDatabase_MySQL5
(我在文件包含部分中包含TeraWurflDatabase_MySQL5.php
而不是TeraWurflDatabase_MySQL5_Mine.php
并public static $DB_CONNECTOR = "MySQL5";
)就没有这样的问题 -
$wurflDbPdoObj = new TeraWurflDatabase_MySQL5();
$wurflDbPdoObj->connect();
$dbObj = $wurflDbPdoObj;
echo "before call";
var_dump($dbObj->rebuildCacheTable());
echo "after call"; //works
我还比较了$rebuilder = new TeraWurfl();
在这两种情况下(新TeraWurflDatabase_MySQL5_mine.php
和现有TeraWurflDatabase_MySQL5.php
)创建的对象的var_dump()
值。发现那里没有问题。
我不知道我的代码出了什么问题。知道吗??
更新
进一步发现问题出在TeraWurfl.php
类的构造函数内部。构造函数再次启动数据库连接器类的对象 -
public function __construct()
{
...
$dbconnector = 'TeraWurflDatabase_'.TeraWurflConfig::$DB_CONNECTOR; //return fails if this object is initiated
if($this->db === false) $this->db = new $dbconnector;
...
}
没有出现错误/警告,与现有的数据库连接器案例相比var_dump()
看起来不错。还会启动该数据库连接器类的对象。
更新 #2 - 分析输出
我按照 http://kpayne.me/2012/02/04/use-register_tick_function-to-profile-your-code/中提到的步骤设置代码分析,稍作修改以在输出中显示类名,以下是输出 -
对测试脚本的分析TeraWurflDatabase_MySQL5_Mine
-
Array
(
[TeraWurfl ->__construct] => Array
(
[time] => 9.0599060058594E-5
[calls] => 8
)
[TeraWurflDatabase_MySQL5_Mine ->__construct] => Array
(
[time] => 4.3630599975586E-5
[calls] => 4
)
[TeraWurflDatabase_MySQL5_Mine ->connect] => Array
(
[time] => 0.00010228157043457
[calls] => 6
)
[TeraWurflDatabase_MySQL5_Mine ->rebuildCacheTable] => Array
(
[time] => 7.8678131103516E-6
[calls] => 1
)
[show_profile] => Array
(
[time] => 1.7881393432617E-5
[calls] => 1
)
)
但是对于现有的类TeraWurflDatabase_MySQL5
,我没有得到函数调用细节,这又很奇怪——
Array
(
[show_profile] => Array
(
[time] => 0.00024199485778809
[calls] => 1
)
)
在这种情况下,即使没有xdebug
,也可以使用以下方法调试流:
declare(ticks=1);
register_tick_function(function(){
$debug = debug_backtrace();
echo "Running on line " . $debug[0]['line'] . " in file " . $debug[0]['file'] . "'n";
});
编辑:实际上,我的例子很糟糕,所以我只留下了重要的部分。