Jump to content

Understanding deprecation and documentation


Go to solution Solved by maxxd,

Recommended Posts

Hi! I don't have permission to post in the Introduction forum, so just very briefly, I'm new around here and very much a novice in php. I've recently decided to take the plunge into really learning how the internet works and I'm building myself a curriculum that I'm thinking will stretch over a couple of years. I've gotten a virtual server set up and started playing about with Wordpress and a few other types of web-based applications and I feel like it's going to be a fun project! 

One thing that's come up that would be very nice to have someone more experienced just point out whether I'm spot on or if I'm misunderstanding is an error message that I think I've managed to reserach the correct solution to. I had this pop up: 

"Deprecated: join(): Passing glue string after array is deprecated. Swap the parameters in /data/bunch/of/path/stuff/tpl_c/file.dashboard_reservation.tpl.php on line 45"

This set me off on a whole little odyssey into the evolving nature of php, and a bit of the process behind it, and formal documentation for implementation. Now, from what I've gathered, I could simply go to the above mentioned file and line, where I find:

<div class="col-sm-
<?php if ($_smarty_tpl->tpl_vars['checkin']->value || $_smarty_tpl->tpl_vars['checkout']->value)  {?>2<?php } 
else 
{ ?>3<?php }?> col-xs-12"><?php echo join($_smarty_tpl->tpl_vars['reservation']->value->ResourceNames, ', ');?>
</div>

(My bold-facing). And then all I have to do is switch like this: 

<div class="col-sm-
<?php if ($_smarty_tpl->tpl_vars['checkin']->value || $_smarty_tpl->tpl_vars['checkout']->value)  {?>2<?php } 
else 
{ ?>3<?php }?> col-xs-12"><?php echo join(', ' ,$_smarty_tpl->tpl_vars['reservation']->value->ResourceNames);?>
</div>

and it should be all good? I'm sure this is nothing to someone who's seasoned in this stuff, but to me this'd be the first real-world problem, as it were, that I've manged to solve, so it'd be so very awesome to get confirmation that I've got the right idea, or if I'm missing something :)

  • Solution

Long story short, yes you are correct. What version of PHP are you using? I'm on 8.4 and got a TypeError instead of a deprecation notice (deprecation means it'll still work in the current version but is slated to be removed from the language and will break your script at a later date).

Just looked at the docs and it appears that passing the separator after the array was removed in 8.0. If you're on anything less than 8.3 I recommend upgrading now - version 8 hit end of life for even security updates almost two years ago.

Edited by maxxd
  • Thanks 1

Thanks for the confirmation, appreciate it! 

And yea, it's currently running on an older version of php (7.4 I think), mostly because it was an old application I found and started practicing with on my virtual server. And hey, it led to me this little path of discovery, so I'm not complaining ^_^

A couple of things to note about whatever application it is:  

It used the Smarty Template Engine.  Smarty was at one time, a very influential library.  It works by performing a "compilation" step where the smarty template files are "compiled" into pure php scripts that are then used at runtime.  

PHP now has two competing MVC frameworks that have pushed use of the language forward, those being Symfony and Laravel.  Each has its own Template system:  Twig for Symfony and Blade for Laravel.  They also perform this compilation step.  While great for it's time, you don't see Smarty used much anymore, as people can use Twig, or use pure PHP templates that intermingle HTML and PHP, and don't require a complicated compilation system.

As for the deprecation, the "join" function is an alias for "implode".  In other words, there are two names for the same function.  Implode tends to be the name used by most PHP developers, as it's the inverse of the well known php function "explode". 

 

<?php

$fruit = 'Pear,Banana,Cantaloup,Grape,Apple';
var_dump($fruit);
// string(33) "Pear,Banana,Cantaloup,Grape,Apple"

$fruits = explode(',', $fruit);

$originalFruits = $fruits;
// sort is a function that "mutates" the $fruits array by sorting it
sort($fruits);
var_dump($fruits);
/*
 array(5) {
  [0]=>
  string(5) "Apple"
  [1]=>
  string(6) "Banana"
  [2]=>
  string(9) "Cantaloup"
  [3]=>
  string(5) "Grape"
  [4]=>
  string(4) "Pear"
}
*/

$fruit = implode(',', $fruits);
var_dump($fruit);
// string(33) "Apple,Banana,Cantaloup,Grape,Pear"

//See if there is any difference in the number 
var_dump($originalFruits);

$diff = array_diff($fruits, $originalFruits);
// Event though the order of elements changed, there are the same elements in both arrays, so the diff of the arrays is empty
var_dump($diff);

// Output
string(33) "Pear,Banana,Cantaloup,Grape,Apple"
array(5) {
  [0]=>
  string(5) "Apple"
  [1]=>
  string(6) "Banana"
  [2]=>
  string(9) "Cantaloup"
  [3]=>
  string(5) "Grape"
  [4]=>
  string(4) "Pear"
}
string(33) "Apple,Banana,Cantaloup,Grape,Pear"
array(5) {
  [0]=>
  string(4) "Pear"
  [1]=>
  string(6) "Banana"
  [2]=>
  string(9) "Cantaloup"
  [3]=>
  string(5) "Grape"
  [4]=>
  string(5) "Apple"
}
array(0) {
}

 

When PHP evolved, an important philosophy was that it was eagerly extended using it's C API.  There has long been numerous libraries written in C, and many PHP contributors set about to write extensions so that those libraries could be used within PHP.  There weren't strict standards or conventions in place as to the recommended way that parameter lists should be implemented, and different developers had different ideas about what makes sense.  People with an axe to grind have criticized PHP for this, but in most cases, they entirely seem to have missed the fact that libraries created to extend the language were not part of the core.

Even with that caveat in mind, implode and explode are unusual, in that they allowed you to provide the primary parameters in different orders.    I guess they finally decided to remove this option, while at the same time adding the "named arguments" feature which provides an alternative method of providing arguments to functions in any order you want, if that is desired.  I think this was inspired by languages like Python that have a similar feature, which in my opinion has limited uses.  The main place where I could see some value in the use of named arguments is in the case of a function having multiple parameters with default values.  The only way you can intermix the use of order parameters and named arguments is when you provide a named argument at the end of an argument list.  

So the one place that named arguments provides something new is in the case where you have a function with a number of required arguements followed by a number of "optional" parameters which were defined to have default values.  Prior to this feature, if you had 2 or more "optional" parameters defined in the function signature, you could not provide an argument to the 2nd -> n parameters, without also providing arguments for the other optional parameters.  

With this named argument feature, you can now just pass a named argument for defaults you want to override.  Having functions with a laundry list of parameters is an anti-pattern, so I don't see this as being a huge improvement to the language, but perhaps others might disagree with me.

 

function test($foo, $bar, $baz='bz', $raz ='rz') {
    echo "$foo $bar $baz $raz" . PHP_EOL;
}

$f = 'Foo';
$b = 'Bar';
$raz = 'Raz';

// Now able to skip over the need for 'baz' parameter, and just pass optional 'raz'
test($f, $b, raz: $raz);

// Output since PHP 8, wasn't available in 7.x
// Foo Bar bz Raz

 

  • Great Answer 1

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.   Restore formatting

  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.



×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.