Page 1 of 1

call a function inside a class

Posted: 26 Sep 2020, 13:41
by gerard
Big regards to all.
I find myself studying a little php to be able to maintain a web page. There is something that I have been working on that I would very much like to be able to do with AHK. I understand that they are different languages, but I can't find a better explanation of what I want to do.
How can I pass parameters and execute a function that I have inside a class
En php

Code: Select all

<?php
class Microondas {
function __construct($color, $potencia) {
$this->color = $color;
$this->potencia = $potencia;
 }

function informacion() {
print("Un microondas de color: ".$this->color.", con potencia ".$this->potencia."<br><br>");
 }
  }

$m = new Microondas("verde", 10);
$m->informacion();
?>
A poorly written example of what I want to be able to understand the concept of class

Code: Select all

Class Speak
 {
Str := "Null"
Hablar(Str)
{
This.Str := Str
MsgBox % Str
 }
 }
 
 f3::
 Texto := New Speak("Probando el uso de las clases")
 % Texto.Hablar()
 Return
How would this be well written?
Thanks in advance

Re: call a function inside a class

Posted: 26 Sep 2020, 18:51
by teadrinker

Code: Select all

Class Speak
{
   __New(str := "") {
      this.str := str
   }
   Hablar() {
      MsgBox % this.str
   }
}
 
F3::
   Texto := New Speak("Probando el uso de las clases")
   Texto.Hablar()
Return

Re: call a function inside a class  Topic is solved

Posted: 26 Sep 2020, 19:07
by A_AhkUser
Hola @gerard,

Code: Select all

% Texto.Hablar()
Here, you must remove the trailing single %: it as a special meaning only in command input parameters - to force an expression:

Code: Select all

Texto.Hablar() ; by default evaluated as an expression in this case by ahk
MsgBox % Texto.Hablar() ; forces the expression in a parameter that does not directly support it
Morevover, you call your method without parameter - yet in the definition of your method, the Str parameter is not optional: the method call silently fails.

The same occurs here:

Code: Select all

Texto := New Speak("Probando el uso de las clases")
...not exactly the same since this time it is question of a superflous parameter; in this case, ahk simply ignores superfluous parameters; at least the call does not fails:

Code: Select all

Texto := New Speak("Probando el uso de las clases")
MsgBox % IsObject(Texto) ; actually shows '1' (true)
 MsgBox % Texto.__Class ; actually shows 'Speak'
The fact remains, however, that, nowhere you define your constructor and its first parameter it should be supplied with, as you done with you hablar method.
Actually, your class lacks its (custom) constructor; in ahk __construct becomes __New, know as a meta-function.
The method must be define in the base object (in plain text in the body of your class or programatically using something like Microondas.__New := Func("myConstructor")).

Spoiler


Cheers

A_AhkUser

Re: call a function inside a class

Posted: 26 Sep 2020, 20:11
by gerard
Thank you very much to both.
A_AhkUser. The explanation is very clear. Thank you very much for that.