-4

Before I ask about problem, I read the suggested link, but it didn't help me.

My problem is when I try to instance a class.

The error is:

Notice: Undefined variable: userCore in /Applications/MAMP/htdocs/proyecto_cice/app/web/controller/controllerFormUser.php on line 74

I know the meaning of this notice, so I use the function class_exists() and I give me true. I used this function, because my first idea was I make a mistake in the class, but it isn't the problem.

I declare this object in load.php file, because I need it in others files too.

I understand the error, but I don't find it. If you can help, I'll be grateful.

The code in the main file where I try instance the class:

<?php
require '../load.php';

function signup($form){
    if( $form['pass']== $form['repass'] ){

    $exists = $userCore->find_mail( filter_project_form() );

    if($exists==''){
        $isCorrect = ($userCore->insert_user( filter_project_form() ));
            if($isCorrect){echo 'correct';}
    }
    else{
        echo "The user exists now";
    }
  }
  else{
    echo 'The passwords must be identical';
  }
}
?>

And the file 'load.php' contains:

<?php
//CONFIG
require 'config.php';
require 'constants.php';

//CORE
require 'core/ddbb.php';
require 'core/user.php';
</code>
//CLASS MODEL
require 'model/class_user.php';

//CONTROLLER
require 'controller/controllerUser.php';

//INSTANCIA
$userCore = new User_core();

And the file core/user.php is:

<?php
class User_core extends DDBB_core{
    function find_mail($form){
       $sql = "SELECT email FROM user WHERE email='".$form['mail']."'";
        return $this->executeSelectQuery($sql);
    }
}

Thanks

user2342558
  • 5,567
  • 5
  • 33
  • 54
PiP
  • 13
  • 5

1 Answers1

1

The variable $userCore is not available in your function, as it is defined in the outer scope. You need to make it available:

[...] 

function signup($form){
    global $userCore;

    [...]

By the way, doing things like this is considered bad practice and it goes against the OOP principles. It would be better if you made siginup a method of the UserCore class, or used some kind of rendering utility class for this, where you instead pass the instance of UserCore as one of the arguments to the function instead.

OptimusCrime
  • 14,662
  • 13
  • 58
  • 96