phpphp-8pchart

pChart not working (failing with errors) in PHP 8.0


I noticed that in PHP 8.0, the excellent PHP library pChart (which I use to draw pie charts) no longer worked. The version I used was 2.1.4, which hadn't been updated since 2014-01-19.

It took a long time to figure out what was going on, because my error logs were simply complaining about trying to read properties from arrays, without spotting the real issue.

Solution posted below.


Solution

  • One of the changes in PHP 8 is that a constructor must look like: __construct(). You cannot simply use the name of the class, like, say, Java.

    pChart 2.1.4 was still using class names as constructor.

    Example, in pPie.class.php, the constructor was:

    function pPie($Object, $pDataObject) {......}
    

    I had to change it to:

    function __construct($Object, $pDataObject) {....}
    

    Also, all of the public variables in the class were defined using the var keyword, which is deprecated. Though not required at this time, I changed them to public.

    Finally, I noticed that some of the public variables had initial values that didn't match up to what they actually were. Again, maybe not required, but just to be safe, I set them to null.

    For example:

    Originally, class pPie had these variables:

    var $pChartObject = array();
    var $pDataObject = array();
    var $LabelPos = "";
    

    I changed them to:

    public $pChartObject = null;
    public $pDataObject = null;
    public $LabelPos = "";
    

    You will need to make similar changes (primarily the __construct() change) to all of the .class.php files that come with pChart.

    EDIT: I posted this as an issue on the github page for pchart, and the developer states that he has implemented the changes: https://github.com/wp-statistics/pchart/issues/1