CakePHP - call function from another function inside the same model

I have a CakePHP model in which several functions work well. Now I'm trying to write a new function that uses several functions that I already wrote. It sounds like it should be easy, but I can't understand the syntax. How can I name these functions that I already wrote in my new?

Example:

<?php public function getHostFromURL($url) { return parse_url( $http.$url, PHP_URL_HOST ); } public function getInfoFromURL($url) { $host = getHostFromURL($url); return $host; } 

Result:

Fatal error: call to undefined function getHostFromURL () in /var/www/cake/app/Model/Mark.php on line 151

I also tried something like:

 <?php public function getHostFromURL($url) { return parse_url( $http.$url, PHP_URL_HOST ); } public function getInfoFromURL($url) { $host = this->Mark->getHostFromURL($url); return $host; } 

But got the same result.

Obviously my functions are much more complicated than that (otherwise I would just use them again), but this is a good example.

+4
source share
2 answers

You just need to call another function, for example:

 $host = $this->getHostFromURL($url); 
+4
source

If the function is in the same controller, you access the functions with

$this->functionName .

The following is the wrong syntax:

 public function getInfoFromURL($url) { $host = this->Mark->getHostFromURL($url); return $host; } 

Must be:

 public function getInfoFromURL($url) { $host = $this->getHostFromURL($url); return $host; } 

If the getFromHostUrl method is in the current class. http://php.net/manual/en/language.oop5.php

+5
source

Source: https://habr.com/ru/post/1413256/


All Articles