namespace definition problem

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)

[b]<?php
namespace \php\pear;

class Product
{
protected $name;

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

}[/b]

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

[b]<?php

include ‘Product.php’;

use xampp\php\pear\Product;

$product_one = new Product(‘colador’);
[/b]
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.

Why are you defining a user defined class in the pear namespace?

Have you tried defining your own namespace and using that rather than causing side effects using a namespace outside of your control?

I put the class in /pear because I have the include_path pointing there.
From php.ini:
include_path=F:\xampp\php\PEAR
I suppose it is not a good practice. But that is a preset of XAMPP
So you are saying that I shoul put the class file anywhere under the project?
Thanks.

That isn’t what namespaces are for and has nothing to do with the include path.

Namespaces are to differentiate classes to prevent collisions. Example:

[php]$d = new DateTime();
$date = new \myApp\DateTime();
[/php]

Here the classes are named the same. It would cause an issue without the namespace, because there are multiple definitions for the same class. Using the namespace, I can specifically state which class I want to use.

I know that. My only problem is with “namespace path definition”.
I am struggling with path definition only.
relative/ absolute and where to start the path.

My project directory is: F:\xampp\htdocs\brackets-dev

Where would you put the class folder and what the namespace would be?

Thank you.

The namespace name, is actually irrelevant. You can use the project name, for instance. The location of a class directory can be whatever you want as well.

htdocs->myApp:
–>classes
----->DB.class.php
–>views
----->home.tpl
–>assets
----->css
----->js
----->images
index.php

Thank you very much!!!
I hat two isssues, one with “include” and the othe one with “namespace”.
It is finally working smoothly thanks to your kindness.

Sponsor our Newsletter | Privacy Policy | Terms of Service