Given the following classes:
<?php
class test{
static public function statfunc()
{
echo "this is the static function<br/>";
$api= new object;
}
}
class traductor
{
public function display()
{
echo "this is the object function";
}
}
test::statfunc();
$api->display();
This does not display the message "this is the static function<br/>"
.
Is there a way to instantiate through a static function and get that object outside?
Thanks... I am inexperienced regarding object programming code.
You should return the object from your static function:
static public function statfunc()
{
$api = new traductor;
return $api;
}
And then store the returned object in a variable that you can use.
$api = test::statfunc();
$api->display();
Your use of declaration is a bit off. Your code results in 2 fatal errors. First, class object is not found, you should replace:
$api= new object;
With
return new traductor;
Being a static class, they perform an action, they don't hold data, hence the static keyword. Down the line when you start working with $this and etc, remember that. You need to return the result to another variable.
test::statfunc();
$api->display();
Should become:
$api = test::statfunc();
$api->display();
See, http://php.net/manual/en/language.oop5.static.php for some more information on static keywords and examples.