Jump to content

OOP variables or array How to return variable


Tsalagi

Recommended Posts

I'm not versed in PHP OOP and I have some code that I need to echo out onto a page. I don't know what the meaning of $ct->something is in this code. Is this an array of some type? How do I echo out the $ct->title onto another page? Use a function call? I just don't know what all of the $ct variables are and how to get to them. Any help is appreciated.

function current_theme_info() {
    $themes = get_themes();
    $current_theme = get_current_theme();
    if ( ! isset( $themes[$current_theme] ) ) {
        delete_option( 'current_theme' );
        $current_theme = get_current_theme();
    }
    $ct->name = $current_theme;
    $ct->title = $themes[$current_theme]['Title'];
    $ct->version = $themes[$current_theme]['Version'];
    $ct->parent_theme = $themes[$current_theme]['Parent Theme'];
    $ct->template_dir = $themes[$current_theme]['Template Dir'];
    $ct->stylesheet_dir = $themes[$current_theme]['Stylesheet Dir'];
    $ct->template = $themes[$current_theme]['Template'];
    $ct->stylesheet = $themes[$current_theme]['Stylesheet'];
    $ct->screenshot = $themes[$current_theme]['Screenshot'];
    $ct->description = $themes[$current_theme]['Description'];
    $ct->author = $themes[$current_theme]['Author'];
    $ct->tags = $themes[$current_theme]['Tags'];
    $ct->theme_root = $themes[$current_theme]['Theme Root'];
    $ct->theme_root_uri = $themes[$current_theme]['Theme Root URI'];
    return $ct;
}

Link to comment
Share on other sites

The '->' is the object-to-member operator, with a 'member' being either a field in the object (object data like a string, integer, etc.) or method of the object (function that acts on the object itself).  So, for something like ct->title, 'title' is a field of the ct object.  In the case of this current_theme_info() function, it's assigning all of these array values to particular fields within the ct object.  After this function is run, if you wanted to output the title, all you'd need to do would be:

 

echo ct->title;

 

Hope this helps.

Link to comment
Share on other sites

Thanks. That works if I want to use it outside of another function, but when I want to use it as a parameter it doesn't work.

I tried putting $ct into an array which I can use print_r to view it but I don't know how to target

$ct->title as a $key $value

pair and then pass that into a variable using a foreach statement. Any suggestions?

Link to comment
Share on other sites

Thanks. That works if I want to use it outside of another function, but when I want to use it as a parameter it doesn't work.

I tried putting $ct into an array which I can use print_r to view it but I don't know how to target

$ct->title as a $key $value

pair and then pass that into a variable using a foreach statement. Any suggestions?

 

You can't use an object member like that unless it's an array itself.

 

You can simply use the title as a function parameter:

 

function someFunction($arg)
{
   // do something
}

someFunction($ct->title);

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.