egorpromo
(Egorpromo)
November 26, 2013, 12:05pm
1
What is the way to create object of class or to use its static methods if I know path to class?
For example I have class with name someClass in @app \myfolder\someClass.php. What I have to do?
And second question. What I have to do if I know namespace of class?
1 Like
samdark
(Alexander Makarov)
November 26, 2013, 12:14pm
2
If root namespace is \app then:
// instance
$object = new \app\myfolder\SomeClass();
$object->method();
// static method
\app\myfolder\SomeClass::method();
or
use \app\myfolder\SomeClass;
// instance
$object = new SomeClass();
$object->method();
// static method
SomeClass::method();
Hi,
when u have this coding in your main.php file…
// autoloading model and component classes
'import'=>array(
'application.models.*',
'application.components.*',
),
Default you will have those classes automatically…
samdark
(Alexander Makarov)
November 26, 2013, 12:25pm
4
There’s no need to do any import in Yii2.
egorpromo
(Egorpromo)
November 26, 2013, 12:28pm
5
If I want to store path to class what I must do?
class myclass extends Object
{
public $class='\app\myfolder\SomeClass';
}
fragoulas
(John Fragkoulis)
November 26, 2013, 1:00pm
6
You will just have to get used to type a bit more with namespaces in Yii2. Don’t try to shorthand everything. This particular thing you are trying to get an answer for cannot be done. Correct usage is as described above.
egorpromo
(Egorpromo)
November 26, 2013, 1:17pm
7
I have found what I must do:
$string='app\folder\myclass';
$myclass = new $string;
$myclass->doit();
Ok. I catch the correct way. But what about aliases? If I have alias ‘@app /folder/myclass’ only what I have to do to create object?
egorpromo
(Egorpromo)
November 26, 2013, 1:23pm
8
And what I have to do to use static method of my class?
<?php
namespace app\folder;
class myclass
{
public function echoit()
{
echo 'You have done it';
}
public static function staticMethod()
{
echo 'It is static method';
}
}
Is the correct way?
$string='app\folder\myclass';
$string::staticMethod(); //Is it correct?
egorpromo
(Egorpromo)
November 27, 2013, 2:22pm
10
I have a little investigation about namespaces and aliases in Yii2.
To use class we can use namespaces style.
For static:
$myclass='app\folder\myclass';
$mymethod='staticMethod';
$result = call_user_func([$myclass, $mymethod]);
For non-static:
$myclass='app\folder\myclass';
$mymethod='nonStaticMethod';
$object=new $myclass;
$object->$mymethod();
But I think we can’t autoload and use classes if we use aliases like @app /folder/myclass.
Rick_39_s
(Rick's)
November 28, 2013, 3:49pm
12
what will happen to the following code?
use \app\folder_a\SameNameClass;
use \app\folder_b\SameNameClass;
$obj = new SameNameClass();//which class will be used to create the instance?
qiang
(Qiang Xue)
November 28, 2013, 5:25pm
13
PHP doesn’t allow this. You have to alias one of the classes to be something different.
Rick_39_s
(Rick's)
November 29, 2013, 3:19am
14
got you, qiang!
thank you for answer, and very glad to meet you! I love the Yii framework and I am using it in my work, it is really great! hope you guys keep doing the great job.
btw, a happy thanks giving day to you and the yii team(maybe a little late…) !
egorpromo
(Egorpromo)
December 3, 2013, 1:07pm
15
Now I’ve found out how use static methods of any class. But I have another question. How to use static property of any class in Yii2 ?
Any suggestions?
egorpromo
(Egorpromo)
December 4, 2013, 1:48pm
17
I’ve made another tests
<?php
namespace app\folder;
class myclass
{
public static $myvar=2013;
public static function getMyVar()
{
return 'Your var is ' . self::$myvar;
}
}
In controller we can do:
$myclass='app\folder\myclass';
echo $myclass::getMyVar(); //For static methods
echo '<br />';
echo $myclass::$myvar; //For static properties
Thanks to Pathfinder and samdark.