Home > Net >  PHP Error Using Abstract Class "Class Not Found"
PHP Error Using Abstract Class "Class Not Found"

Time:10-12

I have a simple script that includes:

  • abstract class
  • regular class
  • and an indes page

for some reason I get this error:

( ! ) Fatal error: Uncaught Error: Class 'Products' not found in 

D:\xampp\htdocs\ds2\classes\GetAllProducts.php on line 4
( ! ) Error: Class 'Products' not found in D:\xampp\htdocs\ds2\classes\GetAllProducts.php on line 4
Call Stack
#   Time    Memory  Function    Location
1   0.4119  410648  {main}( )   ...\index.php:0
2   0.4129  414904  include_once( 'D:\xampp\htdocs\ds2\classes\GetAllProducts.php' )    ...\index.php:3

enter image description here This is my code:

index.php

include_once 'abstract/Products.php';
include_once 'classes/GetAllProducts.php';


$customer = new GetAllProducts();
$customer->allData();

abstract/Products.php

<?

    abstract class Products {
        public $data = array(
            array(
                'id' => 1,
                'title' => 'title 1',
                'desc' => 'desc 1'
            ),
            array(
                'id' => 2,
                'title' => 'title 2',
                'desc' => 'desc 2'
            ),
            array(
                'id' => 3,
                'title' => 'title 3',
                'desc' => 'desc 3'
            )
        );
     
    }

classes/GetAllProducts.php

<?php

class GetAllProducts extends Products {

    public function allData() {
        $this->data;
        foreach($this->data as $product){
            echo '
            <article id="product-'.$product['id'].'" class="product">
                <h2 product__title="">'.$product['id'].'</h2>
                <picture product__image="'.$product['title'].'">
                    <source media="(min-width:768px)" srcset="https://picsum.photos/200/300">
                    <img src="https://picsum.photos/200" alt="Flowers" style="width:auto;">
                </picture>
                <button class="product__button" role="button">לצפיה במוצר</button>
            </article>';
        }
    }
}

CodePudding user response:

You have to include the abstract class into the GetAllProducts.php file because you are using the abstract class in that file, not in index.php.

<?php
include_once 'abstract/Products.php';

class GetAllProducts extends Products {}

CodePudding user response:

What solved the issue was that abstract/Products.php file began with <? and not <?php

  • Related