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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.