Why dont create Object so? - php

Before I could create an object in this way in PHP:
use myFolder\models\Document;
$nameClass = 'Document';
$model = new $nameClass;
This used to work without problems.
But now it gives me a error
Is it for the PHP version or configuration?
What is the problem?
Why it sentence works fine?
$model = new Document;
and the previous one fails?

Thanks
The problem was the namespace; it should have been
use myFolder\models\Document;
$nameClass = 'myFolder\models\Document';
$model = new $nameClass;

Related

initiate an instance from variable name and namespace in one line

$map_class_name = __NAMESPACE__.'\\'.$map_class_name;
$map = new $map_class_name;
Is it possible to do this in one line like this?
$map = new (__NAMESPACE__.'\\'.$map_class_name);
The above gives
syntax error, unexpected '('
The answer is no, you can't use new with string or expression.
But you can use
$map = new $map_class_name;
Since you use class from current namespace(__NAMESPACE__) you don't have to prefix classname, namespace will be implied.
Alternative solutions
You might use Reflection
$map = (new \ReflectionClass(__NAMESPACE__."\\$map_class_name"))->newInstance();
Another way is to not break line, but I guess this misses the point.
$map_class_name = __NAMESPACE__.'\\'.$map_class_name; $map = new $map_class_name;
IIFE is also possible
$map = (function($n){return new $n;})(__NAMESPACE__."\\$map_class_name");

Yii using a variable with an IN condition

I am trying to pull information into a page using my model. The issue is that I need to use an IN condition on my mysql using a variable.
Here is the code I use currently
$list_id = '1,3';
$clients = ListSubscriber::model()->findAll(array('condition'=>'list_id IN (:list_id)','params'=>array(':list_id'=>$list_id)));
I won't necessarily know how many numbers will be stored within $list_id, hence the need for a variable to work with the IN.
The code does execute without errors, but only seems to return the values for the first number of $list_id, so in this case it only finds users where the list_id = 1.
Any help is appreciated. I have found this question Yii addInCondition
However they are using static values, which does not resolve my issue.
When I do use static values, the code executes with results as expected.
You can use addInCondition :
$list_id = '1,3';
$criteria = new CDbCriteria();
$arr_list_id = explode(",",$list_id);
$criteria->addInCondition("list_id ", $arr_list_id );
$clients = ListSubscriber::model()->findAll($criteria);
$list_ids = array(1,3);
$clients = ListSubscriber::model()->findAllByAttributes(array('list_id'=>$list_ids));

Use keyword and dynamic class names

use my\Project\FooClass;
$obj = new FooClass(); // ok
$name = 'FooClass';
$obj2 = new $name(); // throws an error that the class wasn't found
Well, I believe the title and the example were pretty enough explanation of my question, so just - why does this throws an error, and how should I deal with this?
Sadly, this is not possible due to the way PHP imports/aliases from namespaces. This can be remedied by using literal namespace definitions, though it no doubt sucks.
As follows:
$r = "my\\Project\\FooClass";
$k = new $r();
There is a patch in the works, or at the very least, it was on PHP's bug report a couple of months back. They will hopefully do something about it.
If it bothers you, you can use class_alias() to remedy it, by the way.
try:
$obj2 = new $name;
Remove the parenthesis
Alternatively:
$obj2 = new {$name}();
Can't explain why this doesn't work. But for how to deal with it:
$name = 'FooClass';
$name = "my\\Project\\FooClass\\" . $name; // prepend namespace
$obj2 = new $name();

Using LocationSearchParameter for TargetingIdeaService v201109

I am trying to use LocationSearchParameter with TargetingIdeaService (v201109). I am struggling with an Invalid_Criterion_ID error. May I request some help with that? Here is how I am setting LocationSearchParameter in php
$locationTargetParameter = new LocationSearchParameter();
$locationTargetParameter->locations=$LocArray; // $LocArray is array of IDs 2840 for US
As of v201702. Here's a working example.
$loc = array();
$location = new location();
$location->setId(2840); // USA
$loc[]=$location;
$locationTargetParameter = new LocationSearchParameter();
$locationTargetParameter->setLocations($loc);
$searchParameters[] = $locationTargetParameter;
Also if you're copying the example file found here. Don't forget to include
use Google\AdsApi\AdWords\v201702\o\LocationSearchParameter;
Since this is needed for the LocationSearchParameter to work.
As for documentation and location IDs, check here.
Here is what worked for me in case there are others looking to do the same thing:
$loc = array();
$location = new location();
$location->id = '2840';
$loc[]=$location;
$locationTargetParameter = new LocationSearchParameter();
$locationTargetParameter->locations=$loc;

Can I dynamically set new object name

I want to create objects dynamically. Right now I'm creating them manually like this
$obj1 = new Prefix_Myobj();
$obj2 = new Prefix_Other();
$obj3 = new Prefix_Another();
How can I set the part after Prefix_ dynamically? I tried this but it didn't work
$name = 'Myobj';
$obj1 = new Prefix_{$name}();
You need to create a string that specifies the entire class name.
$name = 'Myobj';
$classname = 'Prefix_'.$name;
$obj1 = new $classname();
However, it might be better design to build a class registry of sorts rather than generating class names on the fly like this.
Why do you need this? Maybe there is a better solution for your problem; multiton or dependency injection etc...
It is important to say that if your class is in a specific namespace, you need to inform the full path. Example:
namespace my\path\app;
class MyClass {
$name = 'Myobj';
$classname = 'my\path\app\Prefix_' . $name;
$obj1 = new $classname();
}

Categories