Jump to content

Uncaugt TypeError while changing PHP from 7.4 to 8.2.7


biber
 Share

Recommended Posts

Hi,

I copied my website malabu.de on a local LAMP-server which runs PHP 8.2.7 and got the following Fatal Error:

Quote

Uncaught TypeError: visit(): Argument #1 ($parent) must be of type Page, HomePage given, called in site/templates/_head.php on line 53 and defined in site/templates/_head.php:36

As I am not very familiar with PHP I do not know, how to fix it. This is my _head.php-file:

<!DOCTYPE html>
<html lang="de">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title><?php echo $page->title; ?></title>
	<meta name="description" content="<?php echo $page->summary; ?>" />
	<link rel="stylesheet" type="text/css" href="<?php echo $config->urls->templates?>styles/malabu.css" />
</head>
<body>

	<!-- navigation -->
<div id="menu">

<h1 class="titel"><a href="/">malabu.de</a></h1>

<nav id="nav">

<ul>
	<?php 

		// top navigation consists of homepage and its visible children
		$homepage = $pages->get('/'); 
		$children = $homepage->children();

		// make 'home' the first item in the navigation
		$children->prepend($homepage); 

/**
 * Recursive traverse and visit every child in a sub-tree of Pages.
 *
 * @param Page $parent root Page from which to traverse
 * @param callable $enter function to call upon visiting a child Page
 * @param callable|null $exit function to call after visiting a child Page (and all of it's children)
 */
function visit(Page $parent, $enter, $exit=null)
{
    foreach ($parent->children() as $child) {
        call_user_func($enter, $child);
        
        if ($child->numChildren > 0) {
            visit($child, $enter, $exit);
        }
        
        if ($exit) {
            call_user_func($exit, $child);
        }
    }
}



visit(
    $pages->get('/')
    ,
    function(Page $page) {
        echo '<li><a class="men" href="' . $page->url . '">' . $page->title . '</a>';
        if ($page->numChildren > 0) {
            echo '<ul>';
        }
    }
    ,
    function(Page $page) {
        echo '</li>';
        if ($page->numChildren > 0) {
            echo '</ul>';
        }
    }
);
		// output an "Edit" link if this page happens to be editable by the current user
		if($page->editable()) {
			echo '<li><a class="men" href="'.$page->editURL.'">Edit</a></li>';
		}

		?>

		<!-- öffne Mailformular -->
		<li><a class="men" href="mailto:info@malabu.de"><img src="<?php echo $config->urls->templates?>styles/brief.gif" width="25" height="15" alt="info@malabu.de" title="schreib mir">
		</a></li>
		
		<!-- search form 
		<form class='search' action='<?php //echo $pages->get('template=search')->url; ?>' method='get'>
			<input type='text' name='q' placeholder='Search' value='' />
			<button type='submit' name='submit'>Search</button>
		</form>
		-->
	</ul>
	</nav>
	</div>

 

_head.php

Link to comment
Share on other sites

Hello,

To explain (if I'm right):
It looks like you are using a custom page HomePage that is correctly instantiated (so we are sure it extends ProcessWire Page class).
But the parameter of type Page in the function "visit(Page $parent)" is not of the correct type for PHP, so it seems that it doesn't recognize the specified Page type.

I bet this is because you didn't include any "namespace" or "use" instruction in your file. Try to add this:

<?php namespace Processwire;

If you are new to PHP you can try PHPStorm IDE for a month, it's a great help for setting up a project with composer and learning PHP.

3 minutes ago, netcarver said:

Just change this line... To this...

But this is hiding the problem, not solving it. 🙂

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...