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

Asked

Viewed 656 times

2

I would like to know how to access an array through a method. I’d like to know that you 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 that 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 advantage and gave a var_dump in the ARRAY and it is returning me NULL. Could you help me?

  • 4

    Like $label is a private method variable(private) labels() you need to give a return because it’s the only way to retrieve that array.

  • 4

    The code doesn’t seem to make any sense. Creating methods that create local variables and do nothing else is of no use. That is why I did not understand the doubt. Try to inform your objective because the code does not show intention to do anything useful. Do you want to do something with these variables? What? Pass the contents of these variables to another place? Actually the code seems to do a lot of things unnecessarily. It might be a wrong impression but you can clarify the purpose of all these functions.

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 the execution of the method, if you need these values later an option is to internalize it as a class member.

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

calling for:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.