Jump to content
Sign in to follow this  
happywire

[SOLVED] PHP, cannot seem to var_dump object/array

Recommended Posts

[solution]

=========================================================

This is probably more PHP than ProcessWire.

<?php namespace ProcessWire;

// ready.php

if ($page->images) {

	foreach ($page->images as $image) {
      
      var_dump($image);
      
    }
}

The var_dump looks like this.

Spoiler

object(ProcessWire\Pageimage)#191 (12) {
  ["url"]=>
  string(60) "/site/assets/files/1016/sunshine.jpg"
  ["filename"]=>
  string(97) "/shared/httpd/intermediatewire/htdocs/site/assets/files/1016/sunshine.jpg"
  ["filesize"]=>
  int(514133)
  ["description"]=>
  string(0) ""
  ["tags"]=>
  string(0) ""
  ["created"]=>
  string(19) "2019-03-24 17:35:53"
  ["modified"]=>
  string(19) "2019-03-24 17:35:53"
  ["filemtime"]=>
  string(19) "2019-03-24 17:35:53"
  ["width"]=>
  int(2667)
  ["height"]=>
  int(1778)
  ["suffix"]=>
  string(0) ""
  ["variations"]=>
  array(18) {
    [0]=>
    string(42) "sunshine.0x260.jpg"
    [1]=>
    string(45) "sunshine.1024x683.jpg"
    [2]=>
    string(45) "sunshine.1140x760.jpg"
    [3]=>
    string(45) "sunshine.1280x853.jpg"
    [4]=>
    string(46) "sunshine.1600x1067.jpg"
    [5]=>
    string(44) "sunshine.180x120.jpg"
    [6]=>
    string(46) "sunshine.1920x1280.jpg"
    [7]=>
    string(46) "sunshine.2048x1365.jpg"
    [8]=>
    string(44) "sunshine.240x160.jpg"
    [9]=>
    string(46) "sunshine.2560x1707.jpg"
    [10]=>
    string(44) "sunshine.320x213.jpg"
    [11]=>
    string(44) "sunshine.425x283.jpg"
  }
}

Now when I do

<?php namespace ProcessWire;

// ready.php

var_dump($image->variations);

var_dump($image['variations']);

I get NULL and bool(false).

Ok for the 'variations' key I see it holds an array, is there the reason I cannot var_dump its values?
Yes I do know that there are methods for this, getAllVariations() and getAllVariations() with $options.

When I do

<?php namespace ProcessWire;

// ready.php

dump($image['filename']);

dump($image->filename);

I get the name of the file just fine. Ok, wait. This is not only when I do that with an $image.

Similar things happen with I try to var_dump a $page for example.

<?php namespace ProcessWire;

// ready.php

var_dump($page);

var_dump($page['created']); // works fine

var_dump($page['hooks']); // bool(false)

var_dump($page['data']); // bool(false)

The var_dump($page) returns this.

Spoiler

object(ProcessWire\Page)#188 (19) {
  ["instanceID"]=>
  int(10)
  ["id"]=>
  int(1016)
  ["name"]=>
  string(7) "gallery"
  ["path"]=>
  string(9) "/gallery/"
  ["status"]=>
  string(0) ""
  ["template"]=>
  string(12) "gallery-page"
  ["parent"]=>
  string(1) "/"
  ["numChildren"]=>
  int(0)
  ["sort"]=>
  int(0)
  ["sortfield"]=>
  string(4) "sort"
  ["created"]=>
  string(32) "2019-03-14 11:20:11 (1 week ago)"
  ["modified"]=>
  string(32) "2019-03-24 17:35:53 (1 hour ago)"
  ["published"]=>
  string(32) "2019-03-14 11:20:11 (1 week ago)"
  ["createdUser"]=>
  string(5) "admin"
  ["modifiedUser"]=>
  string(5) "admin"
  ["isLoaded"]=>
  int(1)
  ["outputFormatting"]=>
  int(1)
  ["hooks"]=>
  array(15) {
    ["Page::addUrl"]=>
    string(59) "PagePathHistory->hookPageAddUrl() in PagePathHistory.module"
    ["Page::removeUrl"]=>
    string(62) "PagePathHistory->hookPageRemoveUrl() in PagePathHistory.module"
    ["Page::editable"]=>
    string(53) "PagePermissions->editable() in PagePermissions.module"
    ["Page::publishable"]=>
    string(56) "PagePermissions->publishable() in PagePermissions.module"
    ["Page::viewable"]=>
    string(53) "PagePermissions->viewable() in PagePermissions.module"
    ["Page::listable"]=>
    string(53) "PagePermissions->listable() in PagePermissions.module"
    ["Page::deleteable"]=>
    string(55) "PagePermissions->deleteable() in PagePermissions.module"
    ["Page::deletable"]=>
    string(55) "PagePermissions->deleteable() in PagePermissions.module"
    ["Page::trashable"]=>
    string(54) "PagePermissions->trashable() in PagePermissions.module"
    ["Page::restorable"]=>
    string(55) "PagePermissions->restorable() in PagePermissions.module"
    ["Page::addable"]=>
    string(52) "PagePermissions->addable() in PagePermissions.module"
    ["Page::moveable"]=>
    string(53) "PagePermissions->moveable() in PagePermissions.module"
    ["Page::sortable"]=>
    string(53) "PagePermissions->sortable() in PagePermissions.module"
    ["before Page::render"]=>
    string(51) "PageRender->beforeRenderPage() in PageRender.module"
    ["Page::render"]=>
    string(45) "PageRender->renderPage() in PageRender.module"
  }
  ["data"]=>
  array(1) {
    ["title"]=>
    string(7) "Gallery"
  }
}

So inside the $page object I only seem to be able to access the keys and values that are not nested.
Anything that is a key and has an array as value will tell me NULL or bool(false).

Why is that and how could I var_dump ['hooks'], ['data'] of a $page or ['variations'] of an $image?

I used to use var_export a lot but get the "var_export does not handle circular references" warning a lot with ProcessWire.
So I started using var_dump but there face borders where I simply cannot seem to access properties of an object.

Share this post


Link to post
Share on other sites

@happywire don't forget that a Page is not an array or similar, but a PHP object with protected and private fields. From the "top" (like print_r($page)) you may see some internal attributes (like the array of hooks), but you can not directly access those fields. To retrieve, for example, the table of hooks, you'll need to use an allowed method, like print_r($page->getHooks()).

Same for any field placed on your template. Their content is not retrieved everytime you see a $page but only after an access. If you do something like $page->someField, then you do not access a member from that object, the Page object intercepts the access operator and retrieves 'someField' from the database, caches its content in the page object and performs any kind of output formatting specified for that particular field.

So, to obtain a list of fields available on a particular page, you'll do a $page->getFields(), which you may use to enumerate all fields and dump their contents:

foreach($page->getFields() as $fldName)
	{
	echo "{$fldName}: {$page->$fldName}<br/>";
	echo '<pre>';
	print_r($page->$fldName);
	echo '</pre>';
	}

Edit:

if you print_r($page) before and after that loop you'll see the embedded ['data'] array populated.

And similar will do for $image->getVariations();

Edited by Autofahrn
  • Like 4
  • Thanks 1

Share this post


Link to post
Share on other sites
53 minutes ago, happywire said:

So I started using var_dump but there face borders where I simply cannot seem to access properties of an object.

Although I don't know what your reasons for using var_dump(), I really recommend that you use the module TracyDebugger instead. It will give you better and cleaner information regarding any ProcessWire objects and their properties.

  • Like 6

Share this post


Link to post
Share on other sites

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.   Paste as plain text instead

  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.

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...