1

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.

2 Answers 2

2

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(); 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your help simple and fast! Thanks for the edit also.
2

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.

1 Comment

Thanks for your help!! I'll take a look at the link you provided with.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.