I got this error message when working with CHtml::resolveName: cannot pass parameter 2 by reference.
here is my simple test code:
<?php
class TestController extends CController {
public function test() {
var_dump(CHtml::resolveName($myModels, 'someAttribute');
exit;
}
}
when I route to this action, it always throws an error. However when I changed the code like this
<?php
class TestController extends CController {
public function valid($model, $attribute) {
return CHtml::resolveName($model, $attribute);
}
public function test() {
var_dump($this->valid);
exit;
}
}
It works!
I have read the documentation for the CHtml::resoveName() method in Yii framework, and it uses argument reference. But I don't know why it throws an error when using the first code, it's actually the same as the second one.
Could you help me understand this situation?
Thank you!
I think you are missing one closing bracket for var_dump function.
<?php
class TestController extends CController {
public function test() {
var_dump(CHtml::resolveName($myModels, 'someAttribute'));
exit;
}
}
?>
One more thing,
You did not use the function CHtml::resolveName correctly. It does not passes variable name, passes its reference.
Check Here
<?php
class TestController extends CController {
public function test() {
$attribute = 'someAttribute';
var_dump(CHtml::resolveName($myModels, &$attribute));
exit;
}
}
?>
Just rewrite like this:
CHtml::resolveName($myModels, $dummy='someAttribute')
This is because the attribute name passed to function call as variable reference. $dummy='someAttribute' creates temporary variable $dummy to pass it.
Related
Im trying to pass objects between classes in code igniter and am currently failing. What am I doing wrong. Let me strt showing the pure.php version
Errors.php
<?php
class Errors
{
public function __construct(){}
public function setError($msg){}
}
OtherClass.php
<?php
class OtherClass
{
public function __construct(Errors $errorObject) {}
public function someMethod() {}
}
Then in my main controller..
Controller.php
<?php
class Main
{
public function __construct()
{
$this->errors = new Errors;
$this->other = new OtherClass($this->errors);
}
}
By doing this. I can add errors as I go to my error Object, across any objects i instantiate from the Main controller.
Now my code igniter version looks like this
/library/Errors.php
<?php
class Errors
{
public function __construct(){}
public function setError($msg){}
}
/library/OtherClass.php
<?php
class OtherClass
{
public function __construct(Errors $errorObject) {}
public function someMethod() {}
}
Then in my main controller..
Controller.php
<?php
class Main extends CI_Controller
{
public function __construct()
{
$this->load->library('Errors');
$this->load->library('OtherClass',$this->errors);
}
}
When I do this I get an error in my OtherClass saying that $errorObject is not an instance of Errors. Why is the object not being passed?
The problem is with $this->load->library which is defined like this.
public function library($library, $params = NULL, $object_name = NULL)
$params is expected to be an array. If it is not then the $params is set to NULL.
To get around this requires a bunch of monkey biz.
class Errors is unchanged but class OtherClass needs to be changed to...
class OtherClass
{
public function __construct($errorObject)
{
var_dump($errorObject[0]); //so we can prove it got passed
}
public function someMethod(){}
}
Note the removal of the type hint Error from the constructor declaration. Also, we access index 0 of the argument. The reason lies in what happens at the controller.
function __construct()
{
parent::__construct();
$this->load->library('Errors');
$this->load->library('OtherClass', [$this->errors]);
}
We have to put $this->error in an array so that load->library() won't mess with it.
The alternative is to not use "The CodeIgniter Way" and use good old fashion `new' instead. The controller then is...
function __construct()
{
parent::__construct();
$this->load->library('Errors');
$this->other = new OtherClass($this->errors);
}
And other class reverts to...
class OtherClass
{
public function __construct(Errors $errorObject)
{
var_dump($errorObject);
}
public function someMethod(){}
}
Now the problem is that without adding an autoloader to the system you wind up with
Fatal error: Class 'OtherClass' not found in ...
This LINK goes to a page that talks about the various ways to add an autoloader to CI. I know this has been answered on SO too. But I'm failing to find it at the moment.
Consider under my cpp\controllers\ I've 5 files like (AController.php, BController.php etc..)
Each controller has its own public variable like this..
AController.php --- public $variable='Testing';
BController.php --- public $variable='Bhuvanesh';
From my app\views\main.php
If A controller is called I need the value Testing. If B controller is called I need Bhuvanesh.
Its possible in yii2? Thanks in advance.
You should read Yii2 Views Guide :
within the view you can get the controller object by the expression $this->context
So, you should simply use this in your view :
$this->context->variable
Why don't you create a getter method with same name?
class AController {
public function getVariable() { return 'A'; }
}
class BController {
public function getVariable() { return 'B'; }
}
class CController {
public function getVariable() { return 'C'; }
}
Then you can call with
$controller->variable
You could use the __construct() magic method? this function gets executed right when the class is used. If you make something like
public function __construct(){
echo $this->var; //echo out whatever you want here.
}
That's how i would do it.
Currently I'm trying to call a function from a string.
This is the function, that I'll call later:
<?php
namespace App\Validation\Options;
class FacebookOptionValidation
{
static public function validate()
{
echo: 'example';
die();
}
}
Here is my controller:
<?php
namespace App\Http\Controllers\Profile;
use App\Validation\Options;
class ProfileUserEditController extends Controller {
public function updateUserOption()
{
$class = 'Options\FacebookOptionValidation';
$class::validate();
}
}
In this case Laravel shows an error:
Class 'Options\FacebookOptionValidation' not found
But when I call my function like this, everything works fine:
use App\Validation\Options;
class ProfileUserEditController extends Controller {
public function updateUserOption()
{
Options\FacebookOptionValidation::validate();
}
}
As mentioned here, it's possible to call a class/function from a string. But in my case it's not possible - neither in the static or non-static variant.
Is that a 'laravel-thing'?
Try call with full namespace
class ProfileUserEditController extends Controller {
public function updateUserOption()
{
$class = 'App\Validation\Options\FacebookOptionValidation';
$class::validate();
}
}
With PHP7 you can even do this:
(App\Validation\Options\FacebookOptionValidation::class)::validate();
One line of code and without using a string
Is there a way to do something like this:
class Test {
if(!empty($somevariable)) {
public function somefunction() {
}
}
}
I know this might not be best practice, but I need to do this for a very specific problem I have, so is there anyway to do it?
I just want that function to be included in the class if that variable (which is tied to a URL param) is not empty. As it is written now, I get Error: syntax error, unexpected T_VARIABLE, expecting T_FUNCTION
Thanks!
It depends on the your specific use case, and I don't have enough info to give a specific answer, but I can think of one possible fix.
Extend the class, using an if statement. Put everything except the one function in AbstractTest.
<?php
abstract class AbstractTest
{
// Rest of your code in here
}
if (!empty($somevariable)) {
class Test extends AbstractTest {
public function somefunction() {
}
}
} else {
class Test extends AbstractTest { }
}
Now, the class Test only has the method somefunction if $somevariable isn't empty. Otherwise it directly extends AbstractTest and doesn't add the new method.
Call the required function if the variable is not empty.
<?php
class Test {
public function myFunct() {
//Function description
}
}
$oTest = new Test();
if(!empty($_GET['urlParam'])) {
oTest->myFunc();
}
?>
class Test {
public function somefunction() {
}
}
is all you need actually.
Please note that a function inside a class is called 'method'.
AFAIK you cannot have a condition out of the method in class scope (if that flows)
Class Test {
if (empty($Var)){
public function Test_Method (){
}
}
}
Will not work. Why not have it constantly exisisting but only call the method when it's needed?
Example:
Class Test {
public function Some_Method(){
return 23094; // Return something for example purpose
}
}
Then from your PHP:
$Var = ""; // set an empty string
$Class = new Test();
if (empty($Var)){
echo $Class->Some_Method(); // Will output if $Var is empty
}
Perhaps you trying to validate a string within OOP scope, then take this example:
Class New_Test {
public $Variable; // Set a public variable
public function Set(){
$This->Variable = "This is not empty"; // When calling, $this->variable will not be empty
}
public function Fail_Safe(){
return "something"; // return a string
}
}
Then out of Scope:
$Class = new New_Test();
if (empty($Class->Variable)){
$Class->Fail_Safe();
} // Call failsafe if the variable in OOP scope is empty
i have got a static function> which is called
regenerateThumbnailsCron()
And I would like to execute this function by GET params, for example>
if($_GET["pass"]=="password")
self::regenerateThumbnailsCron();
But if I tryied to call this function in constructor>
class AdminImages extends AdminTab
...
public function __construct()
{
if($_GET["pass"]=="password")
self::regenerateThumbnailsCron();
}
I cannot execute this function.
Is any way, how to call this function before __construct to correctly execute?
Thanks very much for any advice.
EDIT>
I tried also with public function>
<?php
include 'AdminImages.php';
$images = new AdminImages();
$images->regenerateThumbnailsCron();
?>
But i got error>
Fatal error: Class 'AdminTab' not found
You need to do a include 'AdminTab.php'; as well, since your class extends that
Not completely sure I understand your question, are you saying that you have static class "B" which extends class "A", "A" having your regenerateThumbnailsCron() method which you want to call before anything else?
If so then try this:
<?php
class A {
private function regenerate() {
.... do something ....
}
}
class B extends A {
function __construct() {
if ($_GET["pass"] == "password") {
parent::regenerate();
}
}
function regenerateThunbnailsCron() {
.... do somethinig ....
}
}
$images = new B();
$images->regenerateThumbnailsCron();
?>
This way, your parent's "regenerate()" function would get called during the constructor. You can switch this around to be a static class if you want, which if your goal is to compartmentalise any variables and functions away from the global scope would be a better way.