I've got a CakePHP model with a few functions in it working well. Now I'm trying to write a new function that uses a few of the functions I've already written. Seems like it should be easy but I can't figure out the syntax. How do I call these functions I've already written within my new one?
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 this (otherwise I'd just reuse them) but this is a good example.
If the function is in the same controller you access functions using
$this->functionName
syntax.
You have wrong syntax below:
public function getInfoFromURL($url) {
$host = this->Mark->getHostFromURL($url);
return $host;
}
Should be:
public function getInfoFromURL($url) {
$host = $this->getHostFromURL($url);
return $host;
}
If getFromHostUrl method is in the current class. http://php.net/manual/en/language.oop5.php
You simply need to call the other function like:
$host = $this->getHostFromURL($url);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With