CodeIgniter Forums
What are the difference between $this->data->total_pages and $data['total_pages'] - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: What are the difference between $this->data->total_pages and $data['total_pages'] (/thread-26687.html)



What are the difference between $this->data->total_pages and $data['total_pages'] - El Forum - 01-20-2010

[eluser]shinokada[/eluser]
Beginner's question.

Could anyone explain the differences between
Code:
$this->data->total_pages

and

$data['total_pages']

My understanding is that the first one is an object way and second way is an array way.

Am I right?

Then what is $this in $this->data->total_pages?

When do I need to use $this->..?

Do I have to use it whenever I use it as an object?

I thank you in advance.


What are the difference between $this->data->total_pages and $data['total_pages'] - El Forum - 01-20-2010

[eluser]danmontgomery[/eluser]
$this is the object in the context that it's being called used (as in "this" instance of the class), and you can only use $this in object methods.

Code:
class blog_object {
  function print_data(){
    echo $this->title . "<br/>";
    echo $this->body . "<br/>";
    echo $this->author . "<br/>";
  }
}

$blog = new blog_object;
$blog->title = "A Blog";
$blog->body = "This is the text of a blog";
$blog->author = "Me";
$blog->print_data();

http://php.net/manual/en/language.oop5.php


What are the difference between $this->data->total_pages and $data['total_pages'] - El Forum - 01-21-2010

[eluser]shinokada[/eluser]
Thanks for your reply.

Why $this->data->total_pages is not $this->total_pages?

What is the reason to add data?


What are the difference between $this->data->total_pages and $data['total_pages'] - El Forum - 01-21-2010

[eluser]bretticus[/eluser]
You really need to read that OOP link.

Hopefully I can provide some basic insight if you understand OOP in PHP well enough.

CodeIgniter runs from one index.php file. For this to work, CI has to load a core object (objects are also called classes.) That core object's properties and methods can be conveniently referred to in your subsequent code using PHP's built in $this variable. That's because all the subsequent classes (objects) are an extension of the core object (see what I mean about reading up on OOP?) It just so happens that CI can load a data object to the core object called data with all the properties and methods you need to do database transactions (thus, $this->data.) total_pages from $this->data->total_pages is simply a property of the data object (although it would appear that it is an extended version of that class since I am not aware of a total_pages property.) I have no idea what it refers to without context. So I can't answer:

Quote:Why $this->data->total_pages is not $this->total_pages?

As for $data['total_pages']...

That is just how you'd refer to the value stored in an array called data with an associate key called total_pages. The data array in CI convention (can be called whatever you like) is most often used to pass an array of data to a view.


What are the difference between $this->data->total_pages and $data['total_pages'] - El Forum - 01-22-2010

[eluser]shinokada[/eluser]
@bretticus, thank you for your reply and time. I appreciate it.

The code I am studying is pryrocms by Phil.

He uses this $this->data->total_pages in controller and in a view as well.

In view,
Code:
<li>
            &lt;?php if($this->data->total_pages == 1)
            {
                echo '1 Page';
            }
            else
            {
                echo $this->data->total_pages . ' Pages';
            }
            ?&gt;
        </li>

Is it common to do it?

I thought $data['total_page'] in controller and use it as $total_page is more CI way.

Is there any benefit using $this->data->total_pages rather than $data['total_page']?