OOP array?

I’m not fully familiar with OOP but this function has me stumped. I’d like to echo out the

$ct->title

I’ve tried putting this into an array which I can print_r and have it print like an array but I cant target the specific title attribute. Thanks in advance

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;
}

Thanks. I’m actually looking for a way to call the function from a theme page and have it echo the title.
Something like this

<?php current_theme_info($ct->title); ?>

I’m not sure how to code it.
Thanks

That is the use of member variables and functions of an object:


$object_name->member_variable;

$object_name->member_function();


Let me correct that for you

$object_name->member_method();

:slight_smile:

The function is a core WordPress function.

add_menu_page( $ct->title . ' Options and Settings', $ct->description ;?>

has to be enclosed in a function due to WordPress and it’s add_action() methods. I think I have to approach this in another way, as in build my own function using the get_themes() and get_current_themes() functions used in the original function. They output arrays.

Are you writing these functions, or is Wordpress?

Because… the function has no parameters, and isnt a class method (which, from the looks of the code, it should be). What stops you from just sticking

add_menu_page( $ct->title . ’ Options and Settings’, $ct->description ;

into the code without the function wrapper? (besides the fact that you’re missing a close ) on that command, i mean)

Thanks. I was needing a good laugh.
Here is what I’m trying to do. The function in my original post is a core function in WordPress.
I want to access the member variables as strings to place into the parameters of another wordpress function. Like this.


echo $ct->title;
function build_toplevel_menu() {
    global $ct, $shortname;
    add_menu_page( $ct->title . ' Options and Settings', $ct->description ;
}

Where there is a $ct is where I would like to access these member functions and can’t. Outside the function using echo as above the function, works fine. Thanks for all your help.

You don’t define $ct in your function, to access the properties of $ct you have 2 options…


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


<?php
function current_theme_info() {
  
    $themes = get_themes();
    if(false === array_key_exists(get_current_theme(), $themes)){
      delete_option( 'current_theme' );
    }
    $theme = $themes[get_current_theme()];
    
    $ct = new ArrayObject(array(), ArrayObject::ARRAY_AS_PROPS);
    
    $ct->name = get_current_theme();
    $ct->title = $theme['Title'];
    $ct->version = $theme['Version'];
    $ct->parent_theme = $theme['Parent Theme'];
    $ct->template_dir = $theme['Template Dir'];
    $ct->stylesheet_dir = $theme['Stylesheet Dir'];
    $ct->template = $theme['Template'];
    $ct->stylesheet = $theme['Stylesheet'];
    $ct->screenshot = $theme['Screenshot'];
    $ct->description = $theme['Description'];
    $ct->author = $theme['Author'];
    $ct->tags = $theme['Tags'];
    $ct->theme_root = $theme['Theme Root'];
    $ct->theme_root_uri = $theme['Theme Root URI'];
    
    return $ct;
}
?>

:slight_smile:


<?php
function current_theme_info() {

I’ll give you a hint. There’s something missing from the second box…coughfunctionwithnoparameterscough

If this function is actually a method of the class, it should be $ct->current_theme_info(); . If you’re trying to call a function passing the object to it, the function definition needs to have a parameter in it; function current_theme_info($ct) {

Okay that explains that we are dealing with an object. Thanks very clear.
Now how do I access this object

$ct->whatever

and have the contents display on the page.
Using the echo function works

echo $ct->whatever

but only if it is outside of a function. I would like to use these “member variables” as parameters that are to be strings. In an array I would use a foreach statement to return a variable, but that’s not an option here. Any suggestions?

try

 
echo $ct->title;

By the way, in OOP, what is all of those lines like

$ct->title

?
That’s not an array. Would this not be better to be in an array?