Call to undefined function : Laravel 5.1 - php

I have a private function as written below in the Controller.
private function GetProjects($ProjectStatus) {
return \App\Models\Project\Project_Model
::where('ProjectStatusID', $ProjectStatus)
->where('WhoCreatedTheProject', auth()->user()->UserID)->get();
}
Below is the action method that is using this private function.
public function ClientCancelledProjects() {
$ProjectStatus = \App\Enumeration\Project\ProjectStatus::Cancelled;
$MyProjects = GetProjects($ProjectStatus);
return view("Project.Client.MyProject", array("Projects" => $MyProjects));
}
Below is the Error coming when running the controller.
Call to undefined function App\Http\Controllers\Project\GetProjects()
Somebody knows why this is happening ? I am trying to reuse some lines of code as they are written many times in the Controller.

To access functions in a controller from a function in the same controller, use self:::
public function ClientCancelledProjects() {
$ProjectStatus = \App\Enumeration\Project\ProjectStatus::Cancelled;
$MyProjects = self::GetProjects($ProjectStatus);
return view("Project.Client.MyProject", array("Projects" => $MyProjects));
}
Note: Self:: (uppercase) will work depending on the version of php installed, but for older versions, self:: is preferred.
Please check this link for more info: PHP - Self vs $this

Functions inside of a class are not global functions, and cannot be called that way. You need to use $this->GetProjects() instead.

Related

Using method from library included by use

I need to use method from Nette library, that I'm including by use command. But it doesn't work as I want to, throws fatal error, that I am calling undefined method.
How should I approach that method to make it work? Stupid question, but I am kinda new to OOP...
Method from class PresenterComponent.php
public function getPresenter($need = TRUE)
{
return $this->lookup('Nette\Application\UI\Presenter', $need);
}
And my code, where I need to use that method:
use Nette\Application\UI\PresenterComponent;
class DatabaseCollectionAdapter extends ArrayDataAdapter
{
// ..... some code......
$this->user = $this->getPresenter()->getUser();
Error:
Fatal Error
Call to undefined method Ctech\Gridator\DataAdapter\DatabaseCollectionAdapter::getPresenter()
change this
$this->user = $this->getPresenter()->getUser();
to this:
$object = new yourObject(); // yourObject extends PresenterComponent
$this->user = $object->getPresenter()->getUser();
use Nette\Application\UI\PresenterComponent; does not include or do any kind of magic to make it's functions available on the fly.
https://secure.php.net/manual/de/language.namespaces.importing.php
It's just a shorthand that helps you to use PresenterComponent directly without specifying the whole namespace.
Your DatabaseCollectionAdapter or ArrayDataAdapter has to have a function that looks like this:
class AdapterClass
public function getPresenter() {
return new Nette\Application\UI\PresenterComponent;
}
}
or something like this
use Nette\Application\UI\PresenterComponent;
class AdapterClass
public function getPresenter() {
return new PresenterComponent;
}
}

Calling php functions that have an instance as an argument

Ok so my question is, i have a Facebook call back function that I'm using. The callback function uses an instance as an argument facebookCallBack(LaravelFacebookSdk $fb)
The function works great,I'm using this in my auth controller in larvel , but now I want to reuse this function within the same controller something like $this>facebookReusableCallBack(LaravelFacebookSdk $fb)
I'm having issues ,i get this error facebookReusableCallBack() must be an instance of SammyK\LaravelFacebookSdk\LaravelFacebookSdk, none given.
here's a sample of my code:
public function facebookCallBack(LaravelFacebookSdk $fb) {
$this->facebookReusableCallBack();
}
public function facebookReusableCallBack() {
//All my code here
}
how can I reuse this function within my controller?
i've tried :
public function facebookCallBack(LaravelFacebookSdk $fb) {
$this->facebookReusableCallBack(LaravelFacebookSdk $fb);
}
but the argument doesn't get passed as an instance?
You don't need to define the type of a variable when you pass it in.
Method definition:
public function facebookReusableCallBack(LaravelFacebookSdk $fb) {
//some code
}
Execution within another method:
function myMethod() {
$var = new LaravelFacebookSdk();
$this->facebookReusableCallBack($var);
}
What you have done is define the type of the variable in your function definition (good) to force the type. However these are only needed in the definition.
So for your explicit example:
public function facebookCallBack(LaravelFacebookSdk $fb) {
$this->facebookReusableCallBack($fb);
}
Because $fb must be an instance of LaravelFacebookSdk when calling facebookCallBack it will be fine for facebookReusableCallBack.

codeception unable to call a function defined in acceptancetester.php from a cest file

Hi I am new to codeception,
Was trying to call a function defined in AcceptanceTester.php from a Cest.
But getting the error return Call to undefined function demoInjection().
Below is my function defination and calling function
public function demoInjection($aLinkText,$sLinkName)
{
$I= $this;
for($z=0;$z<=Count($aLinkText);$z++)
{
if ($aLinkText[$z] = $sLinkName){
$I->Click($aLinkText[$z]);
}
}
return $this;
}
Calling function
public function checkGrabmultiple(AcceptanceTester $I)
{
$I->click('Online Games');
$aLinkText = $I->grabMultiple("//div[#class='col']",'class');
$LinkName = 'Gold Spinner';
$I->debug(demoInjection($aLinkText,$sLinkName));
}
The right way to call a helper method is
$I->demoInjection($aLinkText,$sLinkName);
because helper method belongs to the Tester and not to the Cest class.
Depending of your version of codeception, you might have to run
php codecept.phar build
after making changes to AcceptanceTester.php.
On later version this is run before executing testes, but you can disable it.
This command ceates a function with your new function/step's name in AcceptanceTesterActions.php.
This will allow codecetion to use your new function in the form of
$I->demoInjection($aLinkText,$sLinkName);
Good luck and happy testing!!

Returning a static Class in PHP

I am working on a backend project. I need to return a static object withing another static object:
Class this_is_a_very_long_class_name
{
public static function call()
{
return self;
}
public static function script_link($link)
{
//doing stuff here...
}
}
Class Main
{
public static function view()
{
// trying to return View object
return this_is_a_very_long_class_name::call();
}
}
and I am trying to use it like this:
Main::view()::script_link('Some script');
So how can I accomplish that?
P.S.: I am not looking for another solution. I am looking for a answer what I asked.
You don't need that.
Use
View::script_link();
Also this is wrong and misleading view()->script_link because script_link is static
Addendum
If you your problem is your class name length I suggest you to create simple wrapper for this.
function createLink($string){
return VERY_LONG_CLASS_NAME_HELLO_PHP_NAMESPACE::script_link($string);
}
this way you just need to createLink();
in php 5.3: return new View(); (instead of return View::self;).
Manual: http://php.net/manual/en/language.oop5.basic.php#example-159
in php 5.2 use ReflectionClass
I think your syntax on the call is wrong. Since it is static, what you are trying to do would look something like this:
Main::view()::script_link('Some script');
Except that would give you a syntax error. Also, since it is static, you don't need to return anything. You should make it two separate calls:
Main::view();
View::script_link("Some script");
It makes no sense to say "I need to return a static object". If the class is defined, then the static object is present and can be accessed.
You just need a variable to hold the class, as a direct call is invalid syntax
Sample:
Class Main
{
public static function view($type)
{
// return some class
switch ($type) {
case "View 2":
return View2;
break;
default:
return View;
}
}
}
$v = Main::view("normal view");
$v::script_link('test');
Are you looking for functionality as late static binding? Which is supported from PHP 5.3. See here: http://php.net/manual/en/language.oop5.late-static-bindings.php

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 postalAddress()
{
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()

Categories