Good day to all.

---------------------INTRODUCTION---------------------

I work with the site engine in php. My study of php began with it. There were no problems, wrote new modules for the site and everything worked. Everything was fine until a few days ago I decided to figure out how the engine works, and how to write a full-fledged site engine from scratch. He began to understand a little and everything seems to be working. But I came across barrier number one.

---------------------PROBLEM---------------------

The problem is as follows. I have this code:

class ShowGeneralPage { public function show() { global $tpl_vars; ShowGeneralPage::tpl_vars_add(array( 'key_1' => 'value_1', 'key_2' => 'value_2' )); echo $tpl_vars; } function tpl_vars_add($display_vars) { global $tpl_vars; ...некий код... $tpl_vars = "некое значение"; } } 

I want to draw attention to the method of calling the function "tpl_vars_add" . I connect to the class from the index.php file. The ShowGeneralPage class is in a different file. When I connect to a class, I automatically call the "show" function. In the "show" function, using the "ShowGeneralPage::tpl_vars_add();" I call the function "tpl_vars_add" . And everything works fine. And if I just write "tpl_vars_add();" then nothing will happen. I call this function in the same class in which I am. How do I get the php handler to call my function by its name, and not by its path in the class? In the site engine, with which I worked this way it was implemented, and worked without any problems. I do not. I suspect that somewhere in that engine it was stated how to handle such moments correctly. What are your thoughts on this? Thank you in advance for your attention.

  • So ? $ this-> tpl_vars_add ()? global - bad - zb ' '
  • $this->tpl_vars_add() does not work. Checked. Why is global bad? On the contrary, it is very convenient if you need this variable to be available on all pages of the site. - intro94 2:42 pm
  • > Why is global bad? On the contrary, it is very convenient if you need this variable to be available on all pages of the site. Why is it bad to cross the road to red? On the contrary - it is very convenient, no need to wait for the green, he took it and went - DreamChild
  • Do you need to static it or what? can you put together a little example on ideone that demonstrates a problem? php.net/manual/ru/language.oop5.late-static-bindings.php - zb '22
  • I have a variable in class. From the same class I need to call her. At the moment, only such a construction "ShowGeneralPage::tpl_vars_add();" , and I need to get a call to this variable through the construction "tpl_vars_add();" , so as not to indicate the name of the class to which this function belongs, as it is inside the same class. - intro94

1 answer 1

If in index.php you call ShowGeneralPage :: show (), then for a call inside show () you need to prescribe a method with a class name for anyone. If the construction in index.php is of the form:

 $ShowGeneralPage = new ShowGeneralPage; $ShowGeneralPage->show(); 

then inside show () you can access tpl_vars_add () with the command $ this-> tpl_vars_add ();

Calling a static method (ShowGeneralPage :: show ()) calls only the method that is specified in the output, without access to the other methods and objects of the class.

  • Thanks for the answer. Here is the call to the class itself: require ('includes / pages / class.ShowGeneralPage.php'); $ pageObj = new ShowGeneralPage; $ pageObj-> show (); Yes, this option works. It's just that the engine I was working with allowed to run the function without the $this->my_function(); command $this->my_function(); . So I wanted to know how to do it manually. - intro94
  • @eicto I repeat: I was not interested in the $this->my_function(); construction $this->my_function(); or self::my_function(); . It works great for me. I was interested in the direct function call using the my_function(); construction my_function(); - intro94
  • one
    To call a function directly, remove it from the class, then it will be accessible by the my_function () construction; But toga is no longer OOP code but procedural. You decide how to write :-) - terantul
  • I understood. Thanks for the answers. Only strange then. The engine of the game that I used, it is written in OOP, and in the same form as I write (I am writing following the example of that engine). And there the function is called just by the construction that I want to use. The structure of the site is exactly the same as mine. Calling class $ pageObj = new ShowGeneralPage; $ pageObj-> show (); and from the show(); function show(); the my_function function is called by the my_function(); construction my_function(); . - intro94