从外部获取 php 类的私有属性

最近把 Typecho 升级到了 1.1 (17.8.17),然后主题部分功能挂了,应该是以前魔改过核心代码导致的,70 说国庆会出一个 Beta 版,看来以后还是不改源代码了 …⁄(⁄ ⁄・⁄ω⁄・⁄ ⁄)⁄…

Typecho 评论页中,如果要获取当前的页码是没有办法的,可能需要调用 $comments->_currentPage ,但是 Widget_Comments_Archive::_currentPageprivate 的。

在不修改源代码的情况下,可以使用 ReflectionClass 来获取。

代码如下:

$reflection = new ReflectionClass($comments);

$currentPage = $reflection->getProperty('_currentPage');
$currentPage->setAccessible(true);
$currentPage = $currentPage->getValue(($comments));

echo $currentPage;