Call to a member function get_type() on a non-object

  • RockmanTV
  • Proficient
  • Proficient
  • RockmanTV
  • Posts: 348

Post 3+ Months Ago

I've been working on a site design for awhile and had the entire thing coded the way I liked. I decided to then port it over to Wordpress. Before porting over, there was no issue. However, when I moved over, my links class starting acting weird and I have no idea why it would only error when I simply changed the directory.

This is the line that's calling the class that's producing the error in my display page:
Code: [ Select ]
writeLink('home', '<img src="'.get_bloginfo('template_directory').'/images/hometab.gif" alt="Home Tab" border="0" width="106" height="34" />').'</li><li>';


Here are my class and function, it errors out over line 51:

Code: [ Select ]
class hyperlink{
    private $url; //Location in the directory; pass value in relation to parent directory.
    private $name; //Name of the link you want to appear in the page.
    private $type; //Type of link (used for styles). Types are: navlink, pagelink, [otherlink]
    private $target; //Where the page should go. Types are: _blank, _parent, _self, _top, [other]
    private $ATTtitle; //Gives a rollover dialogue box to tell about the link.
    
    function set_url($new_url){
        $this->url = $new_url;
    }
    function set_name($new_name){
        $this->name = $new_name;
    }
    function set_type($new_type){
        $this->type = $new_type;
    }
    function set_target($new_target){
        $this->target = $new_target;
    }
    function set_ATTtitle($new_target){
        $this->ATTtitle = $new_target;
    }
    function get_url(){
        return $this->url;
    }
    function get_name(){
        return $this->name;
    }
    function get_type(){
        return $this->type;
    }
    function get_target(){
        return $this->target;
    }
    function get_ATTtitle(){
        return $this->ATTtitle;
    }
    
    function __construct($link_url,$link_name){
        $this->url = $link_url;
        $this->name = $link_name;
    }
}

$home_HL = new hyperlink("home","home");
$home_HL->set_ATTtitle('My Homepage');

function writeLink($title,$obj=NULL,$ATTtitle=NULL,$class=NULL,$anchor=NULL,$target=NULL){
    $title = strtolower($title).'_HL';
    $obj = empty($obj) ? $GLOBALS[$title]->get_name() : $obj;
    if(!is_null($GLOBALS[$title]->get_type()) && empty($class)){
        $class = $GLOBALS[$title]->get_type();
    }
    if(!is_null($GLOBALS[$title]->get_ATTtitle()) && empty($ATTtitle)){
        $ATTtitle = $GLOBALS[$title]->get_ATTtitle();
    }
    if(!is_null($GLOBALS[$title]->get_target()) && empty($target)){
        $target = $GLOBALS[$title]->get_target();
    }
    if(!empty($ATTtitle)){
        $ATTtitle = " title='$ATTtitle'";    
    }
    if(!empty($class)){
        $class = " class='$class'";
    }
    if(!empty($anchor)){
        $anchor = "#$anchor";
    }
    if(!empty($target)){
        $target = " target='$target'";
    }
    
    return "<a href='".$GLOBALS[$title]->get_url().$anchor."'$ATTtitle$class$target>".$obj."</a>";
}
  1. class hyperlink{
  2.     private $url; //Location in the directory; pass value in relation to parent directory.
  3.     private $name; //Name of the link you want to appear in the page.
  4.     private $type; //Type of link (used for styles). Types are: navlink, pagelink, [otherlink]
  5.     private $target; //Where the page should go. Types are: _blank, _parent, _self, _top, [other]
  6.     private $ATTtitle; //Gives a rollover dialogue box to tell about the link.
  7.     
  8.     function set_url($new_url){
  9.         $this->url = $new_url;
  10.     }
  11.     function set_name($new_name){
  12.         $this->name = $new_name;
  13.     }
  14.     function set_type($new_type){
  15.         $this->type = $new_type;
  16.     }
  17.     function set_target($new_target){
  18.         $this->target = $new_target;
  19.     }
  20.     function set_ATTtitle($new_target){
  21.         $this->ATTtitle = $new_target;
  22.     }
  23.     function get_url(){
  24.         return $this->url;
  25.     }
  26.     function get_name(){
  27.         return $this->name;
  28.     }
  29.     function get_type(){
  30.         return $this->type;
  31.     }
  32.     function get_target(){
  33.         return $this->target;
  34.     }
  35.     function get_ATTtitle(){
  36.         return $this->ATTtitle;
  37.     }
  38.     
  39.     function __construct($link_url,$link_name){
  40.         $this->url = $link_url;
  41.         $this->name = $link_name;
  42.     }
  43. }
  44. $home_HL = new hyperlink("home","home");
  45. $home_HL->set_ATTtitle('My Homepage');
  46. function writeLink($title,$obj=NULL,$ATTtitle=NULL,$class=NULL,$anchor=NULL,$target=NULL){
  47.     $title = strtolower($title).'_HL';
  48.     $obj = empty($obj) ? $GLOBALS[$title]->get_name() : $obj;
  49.     if(!is_null($GLOBALS[$title]->get_type()) && empty($class)){
  50.         $class = $GLOBALS[$title]->get_type();
  51.     }
  52.     if(!is_null($GLOBALS[$title]->get_ATTtitle()) && empty($ATTtitle)){
  53.         $ATTtitle = $GLOBALS[$title]->get_ATTtitle();
  54.     }
  55.     if(!is_null($GLOBALS[$title]->get_target()) && empty($target)){
  56.         $target = $GLOBALS[$title]->get_target();
  57.     }
  58.     if(!empty($ATTtitle)){
  59.         $ATTtitle = " title='$ATTtitle'";    
  60.     }
  61.     if(!empty($class)){
  62.         $class = " class='$class'";
  63.     }
  64.     if(!empty($anchor)){
  65.         $anchor = "#$anchor";
  66.     }
  67.     if(!empty($target)){
  68.         $target = " target='$target'";
  69.     }
  70.     
  71.     return "<a href='".$GLOBALS[$title]->get_url().$anchor."'$ATTtitle$class$target>".$obj."</a>";
  72. }


Anybody know why there would be a problem? All I did was move directories? Thanks in advance!
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13504
  • Loc: Florida

Post 3+ Months Ago

Compare the setting for register_globals from the old directory and the new directory.
  • RockmanTV
  • Proficient
  • Proficient
  • RockmanTV
  • Posts: 348

Post 3+ Months Ago

Thanks for your reply, I will check into that. I knew it had something to do with the globals statement but I did not know that globals could be turned off.

The reason I'm using the global statement is because I couldn't seem to get title to act like a $home_HL handler without it. Is there another function I could use in place of globals? Similar to a JavaScript eval? The PHP eval seemed to do something different than what I was wanting. The only reason I ask this is because I've been researching this globals issue and apparently it's a security threat and since Wordpress is not software I've written I'd prefer to leave as much security on as I can.
  • RockmanTV
  • Proficient
  • Proficient
  • RockmanTV
  • Posts: 348

Post 3+ Months Ago

I added an .htaccess file with the following line in my directory:
Code: [ Select ]
php_flag register_globals on

and no dice.

I'm really at a loss as to what this problem could be. Is there any other method of doing what I'm doing with the globals array there?
  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Posts: 2733
  • Loc: Nashville, TN

Post 3+ Months Ago

Check your php.ini for a line something like 'register_globals = Off', change to 'register_globals = On', and give your server a restart.
  • RockmanTV
  • Proficient
  • Proficient
  • RockmanTV
  • Posts: 348

Post 3+ Months Ago

Unfortunately I do not host my own server. I'm with Dreamhost. Is there some sort of error with my class that could be causing this error? I'm starting to think that it may not be a register_globals issue...

EDIT: Or maybe it is, Dreamhost seems to have it on by default, but wordpress has the following function in it's wp-settings.php
Code: [ Select ]
function wp_unregister_GLOBALS() {
    if ( !ini_get('register_globals') )
        return;

    if ( isset($_REQUEST['GLOBALS']) )
        die('GLOBALS overwrite attempt detected');

    // Variables that shouldn't be unset
    $noUnset = array('GLOBALS', '_GET', '_POST', '_COOKIE', '_REQUEST', '_SERVER', '_ENV', '_FILES', 'table_prefix');

    $input = array_merge($_GET, $_POST, $_COOKIE, $_SERVER, $_ENV, $_FILES, isset($_SESSION) && is_array($_SESSION) ? $_SESSION : array());
    foreach ( $input as $k => $v )
        if ( !in_array($k, $noUnset) && isset($GLOBALS[$k]) ) {
            $GLOBALS[$k] = NULL;
            unset($GLOBALS[$k]);
        }
}
  1. function wp_unregister_GLOBALS() {
  2.     if ( !ini_get('register_globals') )
  3.         return;
  4.     if ( isset($_REQUEST['GLOBALS']) )
  5.         die('GLOBALS overwrite attempt detected');
  6.     // Variables that shouldn't be unset
  7.     $noUnset = array('GLOBALS', '_GET', '_POST', '_COOKIE', '_REQUEST', '_SERVER', '_ENV', '_FILES', 'table_prefix');
  8.     $input = array_merge($_GET, $_POST, $_COOKIE, $_SERVER, $_ENV, $_FILES, isset($_SESSION) && is_array($_SESSION) ? $_SESSION : array());
  9.     foreach ( $input as $k => $v )
  10.         if ( !in_array($k, $noUnset) && isset($GLOBALS[$k]) ) {
  11.             $GLOBALS[$k] = NULL;
  12.             unset($GLOBALS[$k]);
  13.         }
  14. }


I also tried rewriting the function using variable variables, i.e.
Code: [ Select ]
$$title = strtolower($title).'_HL';
    $obj = empty($obj) ? ${$title}->get_name() : $obj;
    if(!is_null(${$title}->get_type()) && empty($class)){
        $class = ${$title}->get_type();
    }
  1. $$title = strtolower($title).'_HL';
  2.     $obj = empty($obj) ? ${$title}->get_name() : $obj;
  3.     if(!is_null(${$title}->get_type()) && empty($class)){
  4.         $class = ${$title}->get_type();
  5.     }


It ended up giving me the same error.
  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Posts: 2733
  • Loc: Nashville, TN

Post 3+ Months Ago

Dreamhost keeps them disabled by default. Don't know that you'll have luck getting them enabled, either. See this wiki entry for details.

I can't really tell what you're attempting to do with the code as I glanced over it (at work, not a lot of time to play right now), but it looks to me like you need an alternate approach.

If you want to share the error text with us, we can probably confirm the issue for you.
  • RockmanTV
  • Proficient
  • Proficient
  • RockmanTV
  • Posts: 348

Post 3+ Months Ago

The error text is this:

Fatal error: Call to a member function get_type() on a non-object in /root/weblog/wp-content/themes/solumlTheme/includes/links.php on line 82

The entire function is listed in my first post in the second code box. The error occurs on line 51 in my code box (because I remove all of my comments) or 82 on my side of things. The error is caused when I try to do what I posted in my first code box in the first post, essentially calling the writeLink function.

To me it seems like variable variables would do the trick, but I'm either not using them correctly in my example above or they're giving me issues as well.
  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Posts: 2733
  • Loc: Nashville, TN

Post 3+ Months Ago

The error is definitely failing over the object. If Dreamhost is allowing the glboals, then that function would def. cause the issue (provided it's being called somewhere). How about adding something to exclude a subset of globals that, say, begin with some prefix that you could then use on globals you needed?

Alternately, you could easily put your variable name in the noUnset array and see if it works then.
  • RockmanTV
  • Proficient
  • Proficient
  • RockmanTV
  • Posts: 348

Post 3+ Months Ago

It seems like Dreamhost is allowing globals considering my script works perfectly fine outside of the wordpress directory.

How would you recommend putting my variables into the noUnset array? Hacking the wordpress script is the easiest, but every time I upgrade Wordpress I'd have to re-hack it.

Is there any way to work the script without using globals? The only reason I am is because I need to add the '_HL' to the title name (to match up with home_HL, about_HL, etc.) and didn't want to put '_HL' in the writeLink function.
  • RockmanTV
  • Proficient
  • Proficient
  • RockmanTV
  • Posts: 348

Post 3+ Months Ago

So I thought and try using the actual object instead of calling it by reference.

In the writeLink function I added:
Code: [ Select ]
echo $home_HL->get_name();

And I got the same error as before whereas it should've printed "home";

I am baffled as to what's wrong. It seems like using globals is indeed one problem, but even when I use the name directly it still errors out indicating another problem.

Post Information

  • Total Posts in this topic: 11 posts
  • Users browsing this forum: No registered users and 57 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.