gabrielrcouto / php-gui

Extensionless PHP Graphic User Interface library
2.24k stars 175 forks source link

what is "function () use ($app) { ... }" #183

Open davekimble2 opened 4 years ago

davekimble2 commented 4 years ago

I have written many apps in wxPHP and wxPython. This is my first in php-gui.

In /examples/03-window/example.php, lines 17- 26:

$application = new Application( ... );
$application->on('start', function () use ($application) { ... } );

What is this strange PHP ? I can only assume it is an anonymous function with some kind of alias (?) What is an alternative, less mysterious, way to write it ?

$application->on('start', "OnInit" );

as in wxPHP, doesn't seem to enter function OnInit().

Luxian commented 4 years ago
 function () use ($application) { ... }

Is an anonymous function.


Why OnInit is not being called I'm not sure. Maybe you need to pass it in a callable form:

// if you want to call instance method $object->OnInit()
$application->on('start', [$object, "OnInit"]);

// if you want to call a static method MyClass::OnInit()
$application->on('start', 'MyNamespace\MyClass::OnInit');

// if you want to call a normal function on_init():
$application->on('start', 'on_init');
still-dreaming-1 commented 4 years ago

The part where it adds use ($application) means it is making the $application variable in scope inside of the anonymous function body, between the curly braces. By default the local variables are not in scope inside an anonymous function, but you can bring specific ones in scope by adding a use with specific variables you want to be in scope. Instead of passing an anymous function, you can pass anything that is callable.

davekimble2 commented 4 years ago

Thank you for your help. After a good night's sleep, my alternative normal function worked OK. The next question is: why use anonymous functions at all, when it only serves to obscure what is required ?

Moving on, all constructor methods seem to require $defaultAttributes, but what this holds is not described anywhere. The documentation says it is an array, but I don't know what goes in it. What would be "default" about those attributes if I have to give them ?

I think I am getting close to what is required, I can construct a window with several controls/objects using the "anonymous function" style, but surely the PHP coding can be much simplified/clearer with the "normal function" style. It all seems to be "look how much explanatory detail you can leave out if you do it this way". Very hard to understand for a beginner.

still-dreaming-1 commented 4 years ago

I agree the default and available attributes and options for many parts of the code should be made more explicit. I think they should be turned into separate named PHP parameters instead of just an array you pass in that can contain anything.

As far as using anonymous functions, what it really requires is a callable. An anonymous function is just one type of callable. If I had to guess, I would say the examples choose to use them because it make the code more declarative and requires less boilerplate. Declarative code allows you to see exactly what will happen rather than to have to follow the bouncing ball of the control flow of the program, which seems appropriate for illustrative example code. I can't think of anything simpler the code could accept than a callable, as PHP doesn't have events built into the language. You have to provide something "callable" it can call when the event "fires". Anonymous functions can sometimes be more readable than referencing a method or function as a callable, when the implementation is very short, or when the detail of what they contain is more clarifying than the concept they encapsulate, which usually means it is not encapsulating a clean concept that can be named well as a method/function name. There is also an arrow functions syntax that can make anonymous functions more readable when they are only one line, even more so if their main point is to return a value. In the case of events, typically returning a value is not the point, so arrow functions will have limited usefulness in this context, but some people might still prefer them.

davekimble2 commented 4 years ago

An instance of Application creates a top level window, which I will call a frame, to distinguish it from other windows. It is in this instance that all the child controls should be setup.

So I have set this up as template.php, with a frame and a number of controls, in my simple style with normal functions, driven by a configuration section of simple data declarations. This template can then used to create new applications just by editing the configuration section and filling in the event handlers. template.php.txt

A version of this has worked, but this version doesn't:

$ php ./php-gui/template.php
FrameCreate
BackgroundCreate
PHP Fatal error:  Uncaught Error: Call to a member function getWindow() on null in /home/dk/vendor/gabrielrcouto/php-gui/src/Components/AbstractObject.php:77
Stack trace:
#0 /home/dk/php-gui/template.php(152): Gui\Components\AbstractObject->__construct()
#1 /home/dk/php-gui/template.php(133): BackgroundCreate()
#2 /home/dk/php-gui/template.php(112): FrameCreate()
#3 {main}
  thrown in /home/dk/vendor/gabrielrcouto/php-gui/src/Components/AbstractObject.php on line 77

I have stared at it for long enough. What is wrong with it ? The InputFile control works, but what is the way to pick up the /path/to/file string ?

$input->getValue() 

gets an array, not the string.

davekimble2 commented 4 years ago

I see in /examples/13-table/example.php another class, \Gui\Components\Table , which is not mentioned in https://gabrielrcouto.github.io/php-gui/dist/#/0.1.1/ and gives "not found" on "new Table();" . Is there is way to get it ? Why the backslashes ?

davekimble2 commented 4 years ago

Is there a mechanism to use scrollbars to view all the text in a TextArea ? All this should be explained in an overview document somewhere.

davekimble2 commented 4 years ago

template.2.php.txt I have fixed up template, back to the way it was, and got Radio and Checkbox working OK. There is still a problem with InputFile:

$input-> getValue()

should get an array of the files selected, but it has an empty array, even though I selected one. Additionally setting the Title of the selection window, doesn't appear on the titlebar. Are these minor bugs ?

Which poses the wider question, "Is php-gui currently being supported/developed ?" wxPHP effectively died when the single support person got too busy to update for PHP-7, and then after a long gap, the new support person completed the update, but didn't complete the documentation, particularly describing the new installation process. This is a big weakness of these projects, as you can't blame anyone for being too busy in their free time.

davekimble2 commented 4 years ago

The Title has magically started appearing on the InputFile TitleBar.
InputFile method of getValue() is still not working - returning zero-length array.

What is this "Disk full" warning, and how do I get more space ?

davekimble2 commented 4 years ago

If a $text->setValue() string contains adjacent spaces, they appear in the box as a single space.

davekimble2 commented 4 years ago

Is php-gui currently being supported/developed ? It is useless like this.