0

I am trying to write a small engine in PHP to handle data but i keep on getting this error:

Fatal error: Class 'Factory\dataHandler' not found in /Applications/MAMP/htdocs/Imperial/lp/php/dataPhraserController.php on line 12

Been now trying to find a way of solving the problem but not luck.....

My code:

DataFactory.php

    <?php

namespace Factory;

class dataHandler{

    protected $firstName;
    protected $lastName;
    protected $email;
    protected $confirmEmail;
    protected $phoneNumber;

    public function setFirstName($firstName)
    {
        $this->firstName = $firstName;
    }

    public function setLastName($lastName)
    {
        $this->lastName = $lastName;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }

    public function setConfirmEmail($confirmEmail)
    {
        $this->confirmEmail = $confirmEmail;
    }

    public function setPhoneNumber($phoneNumber)
    {
        $this->phoneNumber = $phoneNumber;
    }

    public function getFirstName()
    {
        var_dump($this->firstName);
    }

    public function getLastName()
    {
        return $this->lastName;
    }

    public function getEmail()
    {
        return $this->confirmEmail;
    }

    public function getPhoneNumber()
    {
        return $this->phoneNumber;
    }
}

dataPharserController.php:

<?php

namespace PhraserController;
use Factory\dataHandler;

class DataPhraser
{
    private $object;

    public function __construct()
    {
        $this->object = new dataHandler;
    }

    public function pharseFirstName()
    {
        if(!isset($_POST['first_name']))
        {
            $this->object->setFirstName($firstName = null);
            var_dump($_POST['first_name']);
        }else{
            $this->object->setFirstName($firstName = $_POST['first_name']);
        }
    }
}

$test = new DataPhraser();
$test->pharseFirstName();

The idea is to collect data from a submitted html form, can someone help me.. thx

Tomazi
  • 787
  • 3
  • 11
  • 30
  • Error says exactly whats wrong. Can you show your `autoload` function, since the file isn't finding the class `Factory\dataHandler` – Darren Nov 26 '14 at 01:08
  • You do know you have to `require` the class file of DataFactory.php? `use Factory\dataHandler` does not do that. BTW: look into PSR-0 (or PSR-4) naming of class files, `dataHandler` != `DataFactory` – Wrikken Nov 26 '14 at 01:09
  • autoloader...? I do not have a autoloader...? – Tomazi Nov 26 '14 at 01:10
  • @Wrikken isynt my class naming right....? – Tomazi Nov 26 '14 at 01:12
  • That's exactly your issue. You **need** to include the file that has the class if you want to use it. An autoloader function does that automatically for you. What you're doing is right, but you need the files to be included. – Darren Nov 26 '14 at 01:21
  • Hi @Darren appreciate your replay. The OPP architecture I am programing with now is new to me, (Namespaces and Use). Could you give me an example where I need to include the file...? top of my `dataPharserController.php` file...? what is the autoloader you mentioned before...? – Tomazi Nov 26 '14 at 01:26
  • [`This`](http://www.php-fig.org/psr/psr-0/) and [`this`](http://stackoverflow.com/q/1830917/2518525) are great examples. – Darren Nov 26 '14 at 01:30

1 Answers1

0

Place this in one of your config files that are loaded before everything

DEFINE('LIB_DIR', '/path_to_library_dir_of_project/');
function AutoloadDefault($ClassName)
{
  $File = LIB_DIR.'/' . $ClassName. '.php';
  // echo 'Default:'.$File.'<br/>';
  if (file_exists($File)) 
  {
      require_once($File);
  }
}
spl_autoload_register('AutoloadDefault');

Then just point to right LIB_DIR folder. spl_autoload_register will load the class file

Ace
  • 21
  • 1