Jump to content

Recommended Posts

Ok, so I don't really understand a lot about OOP. I was given a two big files full of classes and functions. We were hand coding this file for each use. My objective was to make an array we could use to ease the pain of coding these features.

 

Here is an example of what is happening to me. Can someone advise me conceptually on what to do.

 

include('Tons of files included here');

$myarray = array(
                             0 => array("question" => "answer"),
                             1 => array("question1" => "answer1")
                  );
class BigClass {
var $a
var $b
function fiststep(){
global $myarray;
                foreach($myarray as $anarray) {
		$this->$anarray = new includedfunction1($anarray["question"]);
		$this->$anarray->Values = array(array("yes", "yes"), array("no", "no"));
		$this->$anarray->Multiple = true;
	}
}
function nthstep(){
global $myarray;
                foreach($myarray as $anarray) {
		$this->$anarray->astep();   // Error occurs: Call to a member function on a non-object
	}
}
}

include('Tons of files included here');

 

There seem to be a number of steps that occur in one of the 10-20 include files before the data comes back to the nthstep. Once it comes back. I cannot seem to get it to be recognized as an object. If I were to manually type out the contents of $anarray. I get NO error. I am very over my head. Does anyone have a conceptual idea of my issue?

Link to comment
https://forums.phpfreaks.com/topic/233601-oop-member-function-on-a-non-object/
Share on other sites

First off, I'm pulling from a hat that's been more used to strongly typed languages for the duration that its been on my head, so correct me if I'm wrong.

 

Wouldn't it be much easier to have passed on your variables as arguements to functions rather than using it as a global instance? There's less chance of altering a needed dataset for the next operation. So instead of modifying AND using your global variable using functions that require the instance at moment of function call, simply have the function return a new value that you can affix to your original variable.

'Global' should never be used, regardless of whether you're writing procedural code or OO code.  Period.  Argument lists exist for a reason.

 

Also, to the OP, remember that classes are merely blueprints for objects.  Classes, themselves, don't execute any code.  You need to create a new object instance, and then use it to invoke its methods.

I agree reidel. I would love to pass the variables into the function. The issue is that I only have two files given to me by my boss. I have no idea where these functions are actually being initially called.

 

Nightslyr, I have read that globals are discouraged for security reasons. As I mentioned I am a novice at OO. I just need to get this thing working. Then I can fix over arching issues with its design. You mentioned creating a new object instance. Would this be the correct way to do that in the example I previously listed:

 

include('Tons of files included here');

$myarray = array(
                             0 => array("question" => "answer"),
                             1 => array("question1" => "answer1")
                  );
class BigClass {
var $a
var $b
function fiststep(){
global $myarray;
                foreach($myarray as $anarray) {
		$this->$anarray = new includedfunction1($anarray["question"]);
		$this->$anarray->Values = array(array("yes", "yes"), array("no", "no"));
		$this->$anarray->Multiple = true;
	}
}
function nthstep(){
global $myarray;
$myarray = (object) $myarray; //object added as per nightslyr's discussion
                foreach($myarray as $anarray) {
		$this->$anarray->astep();   // Error occurs: Call to a member function on a non-object
	}
}
}

include('Tons of files included here');

 

I tested this out, and I received the exact same error. I clearly don't understand something.

I believe this is what Nightslyr mean by creating and using object instances rather than class definitions:

Class definitions are where you define an object. An object instance is an instance of a class or data type, in this case an instance of your class.

include('Tons of files included here');

$myarray = array(
                             0 => array("question" => "answer"),
                             1 => array("question1" => "answer1")
                  );
class BigClass {
var $a
var $b
function fiststep(){
global $myarray;
                foreach($myarray as $anarray) {
		$this->$anarray = new includedfunction1($anarray["question"]);
		$this->$anarray->Values = array(array("yes", "yes"), array("no", "no"));
		$this->$anarray->Multiple = true;
	}
}
function nthstep(){
global $myarray;
$myarray = $myarray;
                foreach($myarray as $anarray) {
		$this->$anarray->astep();
	}
}
}//definition of the class BigClass ends here

$objectInstance = new BigClass;
//once you've created the object instance of BigClass only then can you use the functions of BigClass
$objectInstance->nthstep();

include('Tons of files included here');

 

Take note: This method still uses the global variable.

Preferably do this:

... //long code definition
function nthstep($myarray){
$myarray = $myarray;
                foreach($myarray as $anarray) {
		$this->$anarray->astep();
	}
return $myarray;
}
... //other code you need to put

 

You don't need to turn your array into an object.  You need to instantiate your BigClass class.  To create an instance, you need to write the following outside of your class definition:

 

$myObj = new BigClass(); // where myObj can have any name you want, since it's a variable

$myObj->firstStep($myarray); // calls the firstStep method of BigClass, passing in $myarray as an argument

 

I strongly suggest you read through examples of how OOP works in PHP.  The online manual has great code examples.  Start here: http://www.php.net/manual/en/language.oop5.basic.php and continue down the list on the left.

 

Finally, 'global' isn't bad because of security issues*, it's bad because it leads to messy, unmaintainable, unextensible, inflexible code.  It's a crutch used by beginners who start learning PHP by reading bad online tutorials.  It's a real bad habit to get into, and should be remedied as soon as possible, not just for your own sake, but for anyone who has to look at or use your code.

 

*The 'global' keyword is not the same thing as the Register_Globals directive that is now, thankfully, turned off by default in PHP.  Register_Globals was very dangerous from a security standpoint because it simply transformed incoming request values into variables.  This means that it was simple for outsiders to poison variables used to protect sensitive areas of a site by simply passing in new GET or POST values.  See: http://php.net/manual/en/security.globals.php

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.