Jump to content

namespace path problem


star2017

Recommended Posts

Hi,

I am getting this error. I dont know how to troubleshoot it. I am using XAMPP on Windows 8.1 machine.

Fatal error: Uncaught Error: Class 'xampp\php\pear\Product' not found in F:\xampp\htdocs\brackets-dev\index.php:11 Stack trace: #0 {main} thrown in F:\xampp\htdocs\brackets-dev\index.php on line 11

 

 

I have a class called Product in F:\xampp\php\pear\Product.php

include_path=F:\xampp\php\PEAR  (php.ini)

 

<?php

namespace \php\pear# convencion: nombre NAMESPACE = nombre CARPETA contenedora

class Product # convencion: nombre CLASE = nombre ARCHIVO
{
    protected $name;

    public function __construct($name)
    {
        $this->name = $name;
    }
}

 

I am trying to instantiate this class in index.php which is in F:\xampp\htdocs\brackets-dev\index.php  contains this code.

 

<?php
 
include  'Product.php';
 
use xampp\php\pear\Product;


$product_one = new Product('colador');

 

I clearly have a issue with namespace definition and possibly use xampp\php\pear\Product

 

I have tried many things. Googling a lot... and nothing.

 

Help would be much much appreciated.

 

Thank you.

Link to comment
Share on other sites

You don't seem to understand the difference between namespaces and paths. The namespace you've chosen for your Product class is

\php\pear

There is no “xampp” anywhere, so your use statement is nonsensical.

 

In any case, both your paths and the namespaces are very poor. Namespaces are supposed to be meaningful. For example, PSR-4 recommends using the top-level namespace for the vendor (e. g. your company). Sub namespaces can then be used to divide the vendor namespace into logical units:

\YourCompany\Models\Product

Why are you putting your own classes into the PEAR directory? Put them into your project into a sensible directory structure, set up an autoloader, and you're done.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.