We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Call to undefined function

I want to create a function in model that return a string. so for example function stringReturn() { print("randomText") }

I created a createAction() in the controller, for example public function createAction() { $user = new Example(); $user -> id = "1"; $user -> name = "john"; result = $user -> save(); if (!$result) { print_r($user -> getMessages()); } }

I created a beforeCreate function inside the model of Example

public function beforeCreate() { $this->return_string = stringReturn(); }

so ultimately, each time i run the URL to create a new user, I want return_string (inside the database) to have the value of "randomText" which is generated from the function above.

however, i am getting the error of Fatal error: Call to undefined function"

I am new to Phalcon framework so please excuse my lack of knowledge. Thank you all.

It's not phalcon problem. Learn php and oop first.

edited Jun '16

where is stringReturn defined? Undefined function means it doesn't exists. You coudl build it in a helper class or you could build it straight into your model as a private method. or you could load it up in a flat file of raw helper functions. of the three i would probably opt for private method...

private function RandomString()
{
    $string = // logic for random string. 
    return $string;
}

This method would obviously mean its a one trick pony. If you wanted to have a global randomString class, you coudl build a helper class and inject it through the DI. Or simply register the namespace and use it as needed.

Such a method could be called as such inside your beforeCreate() function.

public function beforeCreate()
{
    $this->return_string = $this->RandomString();
}

$this Wojciech is right you should good Object Oriented Programming :).