Jump to content

Leaderboard

Popular Content

Showing content with the highest reputation on 02/17/2022 in all areas

  1. If you want, you can filter them out and focus more on your essentials. But first things first. There is a whole list of different PHP errors, warnings and hints (notices). The one we are interested in is E_DEPRECATED. These messages indicate that a used PHP function somewhere in the code is no longer supported in a future, i.e. higher PHP version, not the actual used one. Example: The days (February 2022) I have changed my developer machine to use the latest PHP version 8.1.2. The last PHP 7.4+ is still supported for 9 months from today on, PHP 8.0 still for 1 year + 9months, PHP 8.1 for 2 years+ 9 months, and all other PHP 8+ versions each plus one more year. So the point is clear now, right? It will be year(s) before PHP 9 is on the schedule. ? Currently in v8.1.2, depending on what code I run, I get notices that certain PHP functions will no longer be available starting with PHP 9. (PHP 9! see above). So currently these functions are safe and supported functions, as in past versions as well as in all further PHP 8+ versions(!). No matter how many additional PHP 8+ versions there will be. ProcessWire by default allows us to suppress ALL output, or show ALL output, by the boolean configuration variable "$config->debug". This is good and right, because on a production site you should always suppress all those outputs. On a developer machine it usually shouldn't bother you if there are also E_DEPRECATED hints included. But if it does, you can filter them out as follows, for example. In your site/config.php you can define a own error handler function and load / bind it on your developer machine only. As a proof of concept or simple example, I added the following function to my site/config.php /** OWN ERROR HANDLER FOR DEPRECATION NOTES **********************************************/ function myLocalDevDeprecationErrorHandler($errno, $errstr, $errfile, $errline) { if(!isset($GLOBALS['DEPRECATION_WARNINGS_BAG'])) { $GLOBALS['DEPRECATION_WARNINGS_BAG'] = []; } if(!is_array($GLOBALS['DEPRECATION_WARNINGS_BAG'])) { $GLOBALS['DEPRECATION_WARNINGS_BAG'] = []; } $GLOBALS['DEPRECATION_WARNINGS_BAG'][] = [$errfile, $errline, $errstr]; return true; // true | false = suppress further processing } /** OWN ERROR HANDLER FOR DEPRECATION NOTES **********************************************/ With the following call, also located in my developer machines site/config.php file, I bind it to all raising E_DEPRECATED warnings ONLY! So we are sure and free from all other types of errors, warnings, hints, etc. // preceed the callback function name with the PW namespace, // and define the error types that should passed to the function set_error_handler('ProcessWire\myLocalDevDeprecationErrorHandler', E_DEPRECATED); (read more on php.net) Now you will not see any of the deprecated notices any more, cluttering your screen. Neither in the front end nor in the backend. But they are all collected in our bag. ? A simple loop in the site/templates/admin.php, before the controller.php is called, can display any of it, maybe to the superusers only, for example: <?php namespace ProcessWire; // IF WE HAVE CACHED PHP DEPRECATION WARNINGS, SHOW THEM IN THE ADMIN, FOR SUPERUSERS ONLY if(isset($GLOBALS['DEPRECATION_WARNINGS_BAG']) && is_array($GLOBALS['DEPRECATION_WARNINGS_BAG']) && 0 < count($GLOBALS['DEPRECATION_WARNINGS_BAG']) && $user->isSuperuser()) { foreach($GLOBALS['DEPRECATION_WARNINGS_BAG'] as $deprecatedMessageItem) { $deprecatedMessageItemMSG = "PHP DEPRECATED (v". PHP_VERSION .") = \n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"; $deprecatedMessageItemMSG .= "{$deprecatedMessageItem[0]} : [{$deprecatedMessageItem[1]}]\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"; $deprecatedMessageItemMSG .= "{$deprecatedMessageItem[2]}"; $this->message($deprecatedMessageItemMSG); } } require($config->paths->adminTemplates . 'controller.php'); If you want to show them on the front end to, it belongs to how you have organized your output strategy. I use a delayed output with _init.php and _main.php, where I have a special function for adding all kind of debug messages, that gets displayed on the end of the site, after the footer. Simply adding the whole collected array is enough for my information: if(isset($GLOBALS['DEPRECATION_WARNINGS_BAG']) && is_array($GLOBALS['DEPRECATION_WARNINGS_BAG']) && 0 < count($GLOBALS['DEPRECATION_WARNINGS_BAG'])) { mvdr(['DEPRECATED ('. PHP_VERSION .')' => $GLOBALS['DEPRECATION_WARNINGS_BAG']]); // add it to the debug output region var } The outputs in admin then may look like this Or the very basic info for me on front end
    4 points
  2. thx @szabesz I checked back and I'm also on 7.4 - sorry for the confusion ? Ok thx then the error is annoying but no issue. Thx for your help!
    2 points
  3. As far as I understand, deprecation warnings are hints to developers that pops up when error level is set to E_ALL or likely, that tell devs that this particular feature will change in future PHP versions. So, this is not an issue! This is a hint to developers who wants to be informed (on their local dev machines). So, you can suppress E_DEPRECATED on your dev machine if you are not interested in such hints. Something like E_ALL & ~E_DEPRECATED https://www.php.net/manual/de/errorfunc.configuration.php#ini.error-reporting I definitely do not alter 3-party-code that is working as expected. ? The lib is from phpclasses.org Manuel Lemos. He updated it last in January 2022. I think he has used and tested it with several PHP versions, at least with 7.4+. ?
    2 points
  4. I am new to PHP and new to PW. The below code works but I get the feeling it could be cleaner/better. Any assistance would be greatly appreciated. <div class="row mb-2"> <?php $items = $pages->find("homepage_item=1"); foreach($items as $item) { // $item === $pages->get($item->id); echo "<div class='col-md-6'>"; echo "<div class='row g-0 border rounded overflow-hidden flex-md-row mb-4 shadow-sm h-md-250 position-relative'> <div class='col p-4 d-flex flex-column position-static'>"; echo "<h3 class='mb-0'>$item->title</h3>"; if($item->arbitrary_publish_date) { echo "<div class='mb-1 text-muted'>$item->arbitrary_publish_date</div>"; } else {echo "<div class='mb-1 text-muted'></div>"; } echo "<p class='card-text mb-auto'>$item->summary</p>"; echo "<a href='$item->url' class='stretched-link'>Continue reading</a>"; echo "</div><div class='col-auto d-none d-lg-block'>"; echo "<svg class='bd-placeholder-img' width='200' height='250' xmlns='http://www.w3.org/2000/svg' role='img' aria-label='Placeholder: Thumbnail' preserveAspectRatio='xMidYMid slice' focusable='false'><title>Placeholder</title><rect width='100%' height='100%' fill='#55595c'/><text x='50%' y='50%' fill='#eceeef' dy='.3em'>Check it out!</text></svg>"; echo "</div>"; echo "</div>"; echo "</div>"; // echo $item->title; } ?> </div>
    1 point
  5. I'm new to both too, but thought I'd chip in with the below as an alternative. May not be optimal, but it's how I've ended up doing things, coming from HTML. <?php $items = $pages->find("homepage_item=1"); ?> <div class="row mb-2"> <?php foreach($items as $item): ?> <div class='col-md-6'> <div class='row g-0 border rounded overflow-hidden flex-md-row mb-4 shadow-sm h-md-250 position-relative'> <div class='col p-4 d-flex flex-column position-static'>"; <h3 class='mb-0'>$item->title</h3> <?php if($item->arbitrary_publish_date): ?> <div class='mb-1 text-muted'><?php $item->arbitrary_publish_date; ?></div> <?php else: ?> <div class='mb-1 text-muted'></div> <?php endif; ?> <p class='card-text mb-auto'><?php $item->summary; ?></p> <a href='$item->url' class='stretched-link'>Continue reading</a> </div> <div class='col-auto d-none d-lg-block'> <svg class='bd-placeholder-img' width='200' height='250' xmlns='http://www.w3.org/2000/svg' role='img' aria-label='Placeholder: Thumbnail' preserveAspectRatio='xMidYMid slice' focusable='false'> <title>Placeholder</title> <rect width='100%' height='100%' fill='#55595c' /> <text x='50%' y='50%' fill='#eceeef' dy='.3em'>Check it out!</text> </svg> </div> </div> </div> <?php endforeach; ?> </div>
    1 point
  6. Thanks Horst for looking into it. If it is a library, then it is a library :) I do not think that any in release version of a library/framework a developer should leave in their code anything spitting out messages no matter what the error reporting settings are. It is because users of libs are interested in their own code issues as well, and suppressing messages is counter-productive. Let's imagine a lib that spits out loads of messages and I have to find among them the ones that were the results of my own code. I sometimes run into this issue with other libs/framework and it is not fun at all. Suppressing the messages does not solve the issue during development so it makes development less fun. Anyway, since PHP 7.4 is history in 9 months and this issue is not a big deal and the best you/we could do is to "complain" to the dev, I can sure live with it. Thanks for you contribution and the module! I always use it in production.
    1 point
  7. Awesome! Thank you, this is far easier for me to wrap my head around. ?
    1 point
  8. How about something like this? This way your editor can recognise the HTML tags as well as the PHP tags. The <?= ?> tag is a PHP shorthand for echo ... that you can insert within HTML. For PHP logic you need the full <?php ... ?> tag. You'll notice I've left a couple of echo statements in because they're short, and it's not really worth closing the PHP tag and opening another one for such short sections of HTML code. <div class="row mb-2"> <?php $items = $pages->find("homepage_item=1"); foreach($items as $item) { // $item === $pages->get($item->id); ?> <div class='col-md-6'> <div class='row g-0 border rounded overflow-hidden flex-md-row mb-4 shadow-sm h-md-250 position-relative'> <div class='col p-4 d-flex flex-column position-static'> <h3 class='mb-0'><?= $item->title?></h3> <?php if($item->arbitrary_publish_date) { echo "<div class='mb-1 text-muted'>$item->arbitrary_publish_date</div>"; } else { echo "<div class='mb-1 text-muted'></div>"; } ?> <p class='card-text mb-auto'><?= $item->summary ?></p> <a href='<?= $item->url ?>' class='stretched-link'>Continue reading</a> </div> <div class='col-auto d-none d-lg-block'> <svg class='bd-placeholder-img' width='200' height='250' xmlns='http://www.w3.org/2000/svg' role='img' aria-label='Placeholder: Thumbnail' preserveAspectRatio='xMidYMid slice' focusable='false'><title>Placeholder</title><rect width='100%' height='100%' fill='#55595c'/><text x='50%' y='50%' fill='#eceeef' dy='.3em'>Check it out!</text></svg> </div> </div> </div> <?php // echo $item->title; } ?> </div>
    1 point
  9. @2hoch11 - please try the latest commit - it should work now even if there is no CkEditor field on the template.
    1 point
×
×
  • Create New...