Jump to content

General question about variable scope


ian0mackenzie
Go to solution Solved by Jacques1,

Recommended Posts

Hello! Hope it's OK to post this here... I've looked in a few places and, while I've found a few variations of my question, I still have trouble filling in the gaps here.

I'm trying to use more classes and more OOP style writing. However I've run into an interesting variable scope scenario and I don't quite understand.

 

I have a class and I want to make a variable of hardcoded values available to each function within the class. The variable is

$allowedTags = array(
	'strong' => array(),    
        'a' => array('href' => array(),'title' => array(),'alt' => array(),'class' => array(),'id' => array()),
	'em' => array(),
);

This is a wordpress thing. It's used to see what HTML tags are allowed in certain form inputs. My question is in two parts.

1. What's the best way to accomplish this? The technique I'm using now is as follows:

if (!class_exists("ClassName")) {

	class ClassName {

		private $allowedTags;

		function __construct() {
			$this->allowed_tags = array(
				'strong' => array(),    
				'a' => array('href' => array(),'title' => array(),'alt' => array(),'class' => array(),'id' => array()),
				'em' => array(),
			);
		} // End __construct()

                function functionN(){
                        //This works!
                        var_dump($this->allowed_tags);
                }

But something is bothering me about this and it leads me to my other question:

2. Why do I have to define these values in __construct instead of at the private $allowedTags; line? I'm clearly missing something about variable scope here.

 

Thanks so much in advance for any input you can provide!

Link to comment
Share on other sites

  • Solution

You've mixed up two naming styles: $this->allowedTags isn't the same as $this->allowed_tags. If you fix this, you can in fact define the attribute in the class body.

 

Is this the best approach? Well, it's difficult to tell given this very abstract code, but hard-coding the tags inside the class means you won't be able to ever change them (unless you change the class definition, of course). It might make more sense the define the tags outside of the class and pass them through the constructor.

Link to comment
Share on other sites

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.