0

I have a class that I call via:

$tagIt_Instance1 = new TagIt;
$tagIt_Instance1->tag_table = 'tags';
// if maintenace is set to true, fill out these (for update)
$tagIt_Instance1->tag_object_table = 'projects'; // the name of your table
$tagIt_Instance1->tag_object_fieldname = 'project_tags'; // the name of your field
$tagIt_Instance1->tag_object_id = isset($_GET['id']) ? $_GET['id'] : ''; // the corresponding id for the row of the table
// end maintenance
if (isset($_GET['tag_it']) && $_GET['tag_it'] == true) {
    $tagIt_Instance1->TagItAjax(
        isset($_GET['action']) ? $_GET['action'] : '', 
        isset($_GET['term']) ? $_GET['term'] : '', 
        isset($_GET['match_id']) ? $_GET['match_id'] : '',
        isset($_GET['tag_object_id']) ? $_GET['tag_object_id'] : ''
    );
}

In the class, in the constructor, I want to access $tag_table

class TagIt
{

    // edit defaults
    private $debug = true;
    /* this will reload the page if a new tag cannot be added. suggested to leave true as otherwise the user
    could submit a page with a tag that is not in the system because of said error. if maintenance is enabled
    then the tag would be removed eventually when matching occurs, but lets keep the db clean */
    private $reloadOnError = true;
    private $confirmationMsgAdd = 'Do you want to add the new tag'; // no question mark or tag identifier, thats handled by jquery
    private $confirmationMsgDel = 'Do you want to delete the tag';
    private $confirmationMsgDel2 = 'from the database as well';
    private $errorNewTag = 'An error occured. Could not process new tag!';
    private $generalErrorMsg = 'An error occured.';

    // do allow users to delete special cases
    private $special_cases = array('featured');

    private $select2JS = '<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/select2/3.5.0/select2.min.js"></script>';
    private $select2CSS = '<link rel="stylesheet" href="/neou_cms/plugins/select2.css" type="text/css" media="all">';

    public $tag_table = '';

    /* enabling this will make sure that only tags in the database are
    kept for a specific row when tagit checks for matches */
    public $maintenance = true;

    // do not edit!!! ///////////////////////

    // if maintenace is set to true these become applicable (leave blank here)
    public $tag_object_table = '';
    public $tag_object_fieldname = '';
    public $tag_object_id = '';

    private $location = '';

    public function __construct()
    {
        // we don't want the query string messing up the AJAX request, thus we remove it
        $this->location = strtok($_SERVER["REQUEST_URI"], '?');

        echo $this->tag_table;
    }

However, whenever I try and access $this->tag_table, I always get an empty variable, despite $this->tag_table working in later functions of the script. I feel like it may be a scope issue, I am not sure.

1 Answer 1

1

Your $this->tag_table is empty in the constructor because it's still referencing the unset/empty variable (public $tag_table = '';).

Which is evident with your code:

$tagIt_Instance1 = new TagIt;
$tagIt_Instance1->tag_table = 'tags';

In the above example you're constructing the object and then you only set $tag_table.

You could modify your __construct() to have the tag table passed to it if you require:

function __construct($tag_table) {
    // we don't want the query string messing up the AJAX request, thus we remove it
    $this->location = strtok($_SERVER["REQUEST_URI"], '?');

    // set the tag table, allowing you to access
    $this->tag_table = $tag_table;

    echo $this->tag_table;
}

Meaning you can skip one step:

$tagIt_Instance1 = new TagIt('tags');

It's in fact, common practice to pass "defining" variables like this through the __construct() method while instantiating a class.

Sign up to request clarification or add additional context in comments.

2 Comments

damn man embarrassing I forgot that, its been a while since I've coded and I've been trying to wrap my head around some of the stuff I wrote a while back haha.
@Alex We've all been there before haha! Remember you could pass all those variables through your __construct() and make your code more maintainable :)

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.