This is not a real autoloading - these files are loaded on every request, even if these functions are never used.
This is not a real autoloading - these files are loaded on every request, even if these functions are never used.
Yes, that is correct.
When changing the whole syntax of Yii2, many will not approach Yii3, the migration will only be mental, the entire application code must be rewritten, in that case it is better to do it again.
Is following not possible?
use Yiisoft/Html;
<?= Html::input($bla, $bla) ?>
Technically it is possible but means that namespace for the package should be just Yiisoft
. I prefer to avoid that and stick to https://github.com/yiisoft/docs/blob/master/004-namespaces.md
Does injecting HTML helper as insatiable object to View
was ever considered?
$this->html->input($bla, $bla)
or
$html->input($bla, $bla)
This could allow to switch between helper implementation without changing view itself (for example switching between TB 3 and TB 4 may require only changing HTML helper used by view component).
Hi,
I was reading and passed my mind something maybe absolutely stupid but, I will say it.
Could it be possible to, try to imagine, as we are using Yii, in all project, that we do not need to declare the use of any particular yii core related package, and it will detect the use and load only what is needed? Sorry, I may be doing Sci-Fi here, but it occurred me.
Yes. During Yii 2 development. Disadvantages were:
$this->html->input()
vs Html::input()
. Considering namespace it’s nearly the same but namespace could be imported once and $this
reference can not. In case of $html
there could be naming conflict since variable name is common.No, it won’t work like that. Try replacing Bootstrap helpers with Bootstrap4 helpers in current Yii 2 project and you won’t get it working immediately. You have to adjust plain HTML with bootstrap classes as well.
Would you please elaborate?
$html->input()
is almost the same as Html::input()
.
$html
could be injected only in there is no such variable used in view - in this case user need to use $this->html->input()
. This could also allow overwriting used helper at controller level - you can inject custom helper on render()
call.
At worst case user could just use $html = $this->html;
at the top of the view to create shortcut with any name - same as import.
I don’t get rest of the points, they don’t look like a problems to me.
This is matter of proper abstraction. Right now in Yii 2 this is not even a goal, because it is impossible due architectural decisions (static calls to helpers - you cannot configure it in any way, so you just continue hardcoding stuff). By using insatiable helper this will become possible and most of bootstrap specific raw HTML could be replaced by constants and or method calls.
@samdark, first, consider me like a newbie as I really don’t understand or know all the working processes of yii and all implemented things yet. I will try to elaborate.
I do lot of self brainstorm, I like to think and consider in first steps, all possibilities, even if they present to be stratosphere bullshit thoughts that will be outed, this is my basic way of starting ideas, so, what occurred to me was:
use yii;
or even not.What I meant was, when you are creating a project in yii you will use it functionalities from core and framework packages, why do we need to declare all of them always?
If Helpers are part of core or part of framework, for example, we should not need to declare it use of.
The core or framework should manage the usage instead of throwing errors only, and auto-declare the use if found and needed.
So if it could be done like I am describing, it should be done by just calling as described for via functions: <?= input($bla, $bla) ?>
And not only for HTML, it could be done for anything from the yii framework, but I don’t know how it could be implemented nor if it would conflicts with other things. Yeah lots of Sci-fi here
Cheers
@muitosefala what you describe is importing functions. Technically it is easy as I’ve mentioned above. No having to import functions means these are global but that creates many possibilities for conflicts especially considering extensions. Namespacing functions is not really possible without use
or fully qualified names.
What you propose makes sense.
View
could have a method such as setDefaultParameters()
that we can configure via container:
\Yiisoft\View\WebView::class => [
'__class' => \Yiisoft\View\WebView::class,
'setDefaultParameters()' => [
'html' => Reference::to(\Yiisoft\Html\Html::class),
],
]
\Yiisoft\Html\Html
should be non-static class then.
We could keep it static - then helper could be used statically without instantiation (Html::input()
) and from instance ($html::input()
).
That sounds interesting but one thing is still the issue. In order to get code completion in IDEs you have to do:
<?php
/** @var \Yiisoft\Html\Html $html */
?>
That is pretty similar effort-wise to importing with use
.
So with the same effort you will get much more possibilities. Is it really an issue?
I’m more concerned about additional complicity and things that could be broken by configuration. For example if we allow to configure list of variables injected to view by setDefaultParameters()
, you cannot be sure that $html
exists and that it contains (correct) HTML helper.
Yea. Many ways to shoot yourself in the leg.
In my oppinion type control which comes from the language itself is superior to the usage of PHPDoc hinting or alike. The problem with PHPDoc is that there is a possibility that it gets out of sync with the actual implementation. I know, IDEs can prevent this to an extent. Still, it can (and will) happen at one point. On the other hand, this can not happen with pure native PHP. If a use-clause or function is not defined, the code simply does not run through. This means for me that generally less PHPDoc is better, provided there is a native equivalent.
With that in mind I’m not sure if importing functions is a good idea: If I understood this right, for every function that should be importable in a view, the composer.json file had to be updated? That would always add an extra non-native step.
You’re talking about runtime errors? Because they’re working the same wherever you use Html::input()
or $html::input()
.
PHPDoc/typehints could be replaced by assert($html instanceof Html::class)
- at least on dev/test environment you should be able verify that type is correct.