PHP : $this->methodcall() gives error and CalledClass:methodcall() works fine
I am trying to call method getDetails()
from another class which in turns calls to methods from its own class(i.e, called class) and it does so by
$this->getAccount()
and $this->getAddress()
and in called class we have methods like $this->getAccount()
and $this->getAddress()
function but when I call
them I get fatal error message as call to undefined method, but when I try calling that method using CalledClassName::getAddress()
and
CalledClassName::getAddress()
than it works fine.
My question is that class which am calling(i.e, calledClass ) will always have use $this->getAddress()
and $this->getAccount()
as am getting this class
information from other team and there are 3 teams that would be calling functions getDetails()
which would internally call getAccount()
and getAddress()
functions and so how should I deal with the issue of $this on myside when am calling getDetails()
function.
Code Example
Calling Class:
CalledClass::getDetails() // Call to getDetails function in CalledClass
CalledClass::
public function p开发者_开发技巧ostalAddress()
{
return array(
'addressId' => $address->addressId,
'city' => $address->city,
'country' => $address->country,
'postcode' => $address->postcode,
'stateOrProvince' => $address->stateOrProvince,
'street' => $address->streetName,
'streetNumber' => $address->streetNrFirst,
'streetSuffix' => $address->streetNrFirstSuffix
);
};
public function getAddress()
{
return $this->postalAddress();
}
public function setAccount($account)
{
$this->account = $account;
}
public function getAccount()
{
return $this->find('account = 1311143','');
}
public function getDetails()
{
$data = array();
$data[$address] = $this->getAddress();
$data[$account] = $this->getAccount();
return $data;
}
So now using the above method it gives me error and so if am using CalledClass::getAddress()
and CalledClass::getAccount()
and it works fine but I cant chang e the code in the calledclass as am calling this function from another team.
Any guidance or suggestions ?
If the function you are trying to call from another class is static, you need to use the ::
(scope resolution operator) to call on it. It is also the same way when trying to access static properties.
Because static methods are callable without an instance of the object created, the pseudo-variable $this is not available inside the method declared as static.
Taken from PHP: Static Keyword
In the meantime for your method call to work without having the ability to modify the other member's code is to use CalledClass::getAddress()
精彩评论