The __PHP_Incomplete_Class Object in PHP PRO

When storing an object in $_SESSION trying to retrieve it in another page we will get an error if the class itself is not available when the session_start() function builds the $_SESSION array. Debugging the $_SESSION we will find that our object is available and is stored as a __PHP_Incomplete_Class object. In order to avoid this problem we better make sure that the class definition is available before we call the session_start() function by calling the require_once\require\include_once\include for getting the class definition in advance (before we call the session_start() function). Getting __PHP_Incomplete_Class object can happen in other similar scenarios, such as when calling the unserialize() function while the class definition is not available.

The following video clip uses a code sample composed of three files in order to demonstrate getting a __PHP_Incomplete_Class object.

The Rectangle.php file includes the definition for the Rectangle class.

<?php

class Rectangle
{
    private $width;
    private $height;
    function __construct($hVal,$wVal)
    {
        $this->setHeight($hVal);
        $this->setWidth($wVal);
    }
    public function setHeight($height)
    {
        if($height>0)
        {
            $this->height = $height;
        }
    }
    public function getHeight()
    {
        return $this->height;
    }
    public function setWidth($width)
    {
        if($width>0)
        {
            $this->width = $width;
        }
    }
    public function getWidth()
    {
        return $this->width;
    }
}

?>

The first.php file instantiates Rectangle and stores the new object into the $_SESSION super global array.

<?php
include_once 'Rectangle.php';
session_start();
$ob = new Rectangle(4,3);
$_SESSION['rec'] = $ob;
?>
<a href='second.php'>second</a>

The second.php file retrieves the Rectangle object back from the $_SESSION super global array and calls various functions on it.

<?php
require_once 'Rectangle.php';
session_start();
var_dump($_SESSION);
$temp = $_SESSION['rec'];
$width = $temp->getWidth();
$height = $temp->getHeight();
echo 'width='.$width.' height='.$height;
?>

Share:

The Visitor Design Pattern

The Visitor Design Pattern

The visitor design pattern allows us to add operations to objects that already exist without modifying their classes and without extending them.

What are Anti Patterns?

Anti Patterns

Unlike design patterns, anti patterns just seem to be a solution. However, they are not a solution and they cause additional costs.

Virtual Threads in Java Professional Seminar

Virtual Threads in Java

The use of virtual threads can assist us with improving the performance of our code. Learn how to use virtual threads effectively.

NoSQL Databases Courses, Seminars, Consulting, and Development

MongoDB Design Patterns Meetup

The use of MongoDB involves with various cases in which we can overcome performance issues by implementing specific design patterns.

image of woman and database

Record Classes in Java

Learn how to define record classes in Java, and when to use record classes in your code. Stay up to date with the new Java features.

Accessibility | Career | Conferences | Design Patterns | JavaScript | Meetups | PHP | Podcasts | Python | Self Learning

Teaching Methodologies | Fullstack | C++ | C# | CSS | Node.js | Angular | Java | Go | Android | Kotlin | Swift | Academy

Front End Development | Scala | Architectures | Cloud | Big Data | Internet of Things | Kids Learn Programming

The Beauty of Code

Coding is Art! Developing Code That Works is Simple. Develop Code with Style is a Challenge!

Skip to content Update cookies preferences