2

I include class definition and create object inside for loop. If I use include_once(class_file.php) then on the second run of the loop I get exactly the same result I got on first one. If I use include(class_file.php) then I get error “Cannot declare class, because the name is already in use in”

I tried to use unset() to clear object before second iteration but it did not help.

Can/Should I include object within loops? What is appropriate solution?

//run.php
$nominals = array("HFG", "LKD");
for ($x=0; $x<count($nominals); $x++) {
    if ( $nominals[$x] == "HFG" ) {
        $login = "mark";
        $pass = "abc";
        $nom = "HFG";
        $num = 8495876;
    } else if ($nominals[$x] == "LKD") {
        $login = "john";
        $pass = "gfd";
        $nom = "LKD";
        $num = 9384757;
    } else {
        exit();
    }
    include_once("class_Funding.php"); 
    include('GET_funding_scrape.php');
}

If condition inside of the for loop determines values of login, pass, nom and num. Which are later used included php files.

If I include class outside of the loop I get error that login, pass, nom and num are undefined.

Maybe instead of if I should use cases?

Above file run.php starts entire script.

//class_Funding.php
class DataInfo {
  protected $login;
  protected $pass;
  protected $nom;
  public function __construct($login, $pass, $nom) {
    $this->login = $login;
    $this->pass = $pass;
    $this->nominal = $nominal;
  }
  public function go() {
    if ($this->login == "mark") {
      return "111111";
    } else if ($this->login == "john") {
      return "222222";
    } else {
      return "err";
    }
  }
}
$object = new DataInfo($login, $pass, $nom);

Above file class_Funding.php contains class definition and initiates $object.

//GET_funding_scrape.php
$new[] = $object->go();
print_r($new);
enter code here
unset($new);

Above file GET_funding_scrape.php gets data from class_Funding.php ads it to array and prints out

My expectation was that on first iteration I get "111111" and second "222222". But I am getting "111111" each time.

12
  • 1) If I understand you correctly, try declaring outside the loop or including the file outside the loop and see what happens. 2) could you post some code? Commented Feb 15, 2019 at 21:38
  • 1
    Can you show some example code to illustrate the problem? It will be easier to answer if there's something more concrete to work with. Commented Feb 15, 2019 at 21:42
  • I think include can be used to repeat some code or markup, not to reload a class, why do you want to reload the same class many times? Commented Feb 15, 2019 at 21:54
  • 1
    Without seeing what is in the included scripts, how can we know what is the problem? Commented Feb 15, 2019 at 23:00
  • 1
    It sounds like you are confusing including the code for a class with creating an object of that class. When you have class Something { ... };, you need to create an instance of it with $something = new Something(); or the like, then use $something in your code. For example, if you have a DoSomething method, after creating your object, you'd use $something->DoSomething(); See: Objects in the PHP manual Commented Feb 15, 2019 at 23:03

3 Answers 3

3

Okay, so your problem is that you are declaring the class multiple times. Declaring is things like

class myClass {
    // Code...
    // etc.
};

You can only do this once in php (or really any language, but we're only talking about php at the moment). However, what you can do multiple times is instantiate the class multiple times like this:

$obj1 = new myClass();
$obj2 = new myClass();
// etc.

This creates a type of data based on the specs you define in the class declaration.

Suggested solution: move the line $object = new DataInfo($login, $pass, $nom); outside of the file you are including and put only this line in the loop instead. Then move the include outside the loop.

Example:

//run.php
$nominals = array("HFG", "LKD");

// declare the class only once:
include_once("class_Funding.php"); 


for ($x=0; $x<count($nominals); $x++) {
    if ( $nominals[$x] == "HFG" ) {
        $login = "mark";
        $pass = "abc";
        $nom = "HFG";
        $num = 8495876;
    } else if ($nominals[$x] == "LKD") {
        $login = "john";
        $pass = "gfd";
        $nom = "LKD";
        $num = 9384757;
    } else {
        exit();
    }
    $object = new DataInfo($login, $pass, $nom);
    include('GET_funding_scrape.php');
}
//class_Funding.php
class DataInfo {
  protected $login;
  protected $pass;
  protected $nom;
  public function __construct($login, $pass, $nom) {
    $this->login = $login;
    $this->pass = $pass;
    $this->nominal = $nominal;
  }
  public function go() {
    if ($this->login == "mark") {
      return "111111";
    } else if ($this->login == "john") {
      return "222222";
    } else {
      return "err";
    }
  }
}
// Comment this out or delete it
// $object = new DataInfo($login, $pass, $nom);
Sign up to request clarification or add additional context in comments.

Comments

3

As others have pointed out, you are confusing several things:

  • Including a file with include, include_once, require, or require_once. This is just a way of breaking up your code to make it easier to manage. Any PHP code can be in the included file, but often it will contain definitions of functions and classes, which you only need to include once.
  • Defining a class with class Foo { ... }. This gives a name to a particular type of object you want to use in your code. You can only declare each class once; it then keeps its definition until the end of the script.
  • Creating an object with new Foo. This creates a new "instance" of the class you defined earlier. Each instance is separate, and can be passed around through your program like any other variable.

The only part you should be repeating in your loop is creating the object: the class definition is the same every time, and the included file should just include that definition and be loaded once.

1 Comment

Thanks. Your last paragraph put me on the right track.
2

Include the class file only once outside of the loop. It makes no sense to include it inside and checking (include_once) if it's included every time.

If you get an error that variables are undefined when including the file outside the loop, there is something wrong with your class. A class shouldn't depend on variables declaration outside of it.

Including a file and creating an instance of a class are different things.

You can learn about classes in php in the php manual.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.