Reputation: 63
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.
Upvotes: 1
Views: 1345
Reputation:
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);
Upvotes: 3
Reputation: 97688
As others have pointed out, you are confusing several things:
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.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.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.
Upvotes: 3
Reputation: 828
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.
Upvotes: 2