How to access an array inside a method in a class?

2

I would like to know how to access an array using a method. I would like to know that you guys think my code is right what I'm trying to do?

class RegisterPostType
{   
    public $name;
    public $singleName;
    public $addNew;
    public $addNewItem;
    public $editNewItem;
    public $newItem;
    public $viewItem;
    public $searchItem;
    public $notFound;
    public $notFoundinTrash;

    private function labels()
    {
        $label = array(
            'name'                  =>     $this->name,
            'singular_name'         =>     $this->singleName,
            'add_new'               =>     $this->addNew,
            'add_new_item'          =>     $this->addNewItem,
            'edit_item'             =>     $this->editNewItem,
            'new_item'              =>     $this->newItem,
            'view_item'             =>     $this->viewItem,
            'search_items'          =>     $this->searchItem,
            'not_found'             =>     $this->notFound,
            'not_found_in_trash'    =>     $this->notFoundinTrash,
            'parent_item_colon'     =>     ''
        );
    }

    public function registerPostType()
    {
        $args = array(
            'labels' => $this->labels()->label,
            'public' => true,
            'publicly_queryable' => true,
            'show_ui' => true,
            'query_var' => true,
            'menu_icon' => get_stylesheet_directory_uri() . '/article16.png',
            'rewrite' => true,
            'capability_type' => 'post',
            'hierarchical' => false,
            'menu_position' => null,
            'supports' => array('title','editor','thumbnail')
        ); 
    }
}

And I'm calling the array this way:

add_action('init', 'postTypes');
function postTypes()
{
    $header = new RegisterPostType;
    $header->name = 'Titulo';
    $header->singleName = 'Titulo';
    $header->addNew = 'Titulo';
    $header->addNewItem = 'Titulo';
    $header->editNewItem = 'Titulo';
    $header->newItem = 'Titulo';
    $header->viewItem = 'Titulo';
    $header->searchItem = 'Titulo';
    $header->notFound = 'Titulo';
    $header->notFoundinTrash = 'Titulo';
    register_post_type('header', $header->registerPostType()->args);
}

I took it and gave it a var_dump in ARRAY and it is returning me NULL. Could you help me?

    
asked by anonymous 09.02.2015 / 18:59

1 answer

1

If the goal is a method to format an array, do not forget to give a return in the desired variable, a method / function without return, returns null, which in php is interpreted as false.

Variables created within methods are not attributes of the class so they lose their value after executing the method, if you need these values later an option is to internalize it as a member of the class.

private function labels()
{
  return array('name' => $this->name ...)
}

call:

public function registerPostType()
{
        $args = array(
            'labels' => $this->labels() //agora sim o método retorna algo.
    
09.02.2015 / 19:28