0

I am trying to capture data from HTML form post in a OOP fashion. I created some classes where no visible errors occur until I call the file in the browser I see this error:

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

my Code:

dataFactory.php:

    <?php

namespace Factory;

class getData{

    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;
    }

}

dataPherserController.php:

    <?php

namespace php\PhraserController;
use Factory;

class DataPhraser extends Factory\getData{

    public function pharseFirstName(){

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

The error states that PHP does not recognise getData class, I am new to namespaces and use pattern and belief I am doing something simple mistake. I know I can use include_once() but I am trying to learn namespaces and use

Tomazi
  • 787
  • 3
  • 11
  • 30
  • Try just extending from `getData` instead of `Factory\getData` – RichardBernards Nov 25 '14 at 13:55
  • 3
    I think you might look into *[Autoloading Classes](http://php.net/manual/en/language.oop5.autoload.php)*. – bzeaman Nov 25 '14 at 13:55
  • Classes are not auto-loaded by default (even when using `namespace` and `use`) To accomplish this, check out PHP Manual: http://php.net/manual/en/language.oop5.autoload.php and for additional standards recommendation, see: http://www.php-fig.org/psr/psr-0/ – wes.hysell Nov 25 '14 at 14:00
  • @RichardBernards I have tried this but it does not work. Well guys I am not sure if the problem comes form namespace and use am just assuming..... – Tomazi Nov 25 '14 at 14:01
  • as i cant answer because its closed, `extends \Factory\getData` will work, the answer its marked as a duplicate of is different .. – exussum Nov 25 '14 at 14:01
  • The answer it's marked as a duplicate of is the correct answer which tackles the actual problem. There's no problem whatsoever in `extends Factory\getData`. – deceze Nov 25 '14 at 14:11
  • Could some one show me an example of how I can use autoload() function for this instance...? – Tomazi Nov 25 '14 at 15:02

0 Answers0