About area and OOP in PHP

I am having trouble understanding how to work with objects.

Specific code:

class first{ class second{ public function widgets(){ $a_variable = $a_value; } #1 } $second = new second; #2 } #3 $first = new first; 
  • If I initialize $a_variable as $a_variable , it is only available inside the function, fix it?
  • If I initialize $a_varialbe as $this->a_variable , it is only available inside the second class, right?
  • Is it possible to initialize $a_variable as $first->second->a_variable ? If so, how would I call it at #1 , #2 and #3 ?
  • Is it possible to initialize $a_varialbe as $this->second->a_variable ? If so, how would I call it at #1 , #2 and #3 ?

As you can see, I'm just confused about how OOP works.


First of all, I want to express how much I appreciate all the help. I have already learned more than enough to consider this issue with overwhelming success.

However, even if it is poorly worded, psuedo-code, and invalid syntax, this code runs.

 class class_1{ public function function_1(){ require('class_2.php'); public function function_2_callback(){ //%%%%%% How do I set a variable here and get the DATA... } $this->class_2 = new class_2("function_2_callback"); } } $class_1 = new class_1; //&&&&&&&&&& Get the DATA here? /* CONTENTS OF class_2.php */ class class_2($callback){ call_user_function($callback); } 

Even if we should consider it as an exercise. Can someone tell me how I should set (@ %%%%%%%%) first and then call the variable (@ & &&& & && &) as shown?

+4
source share
4 answers

Firstly: what doesnโ€™t work there, you cannot declare a class inside a class the way you do it (despite conditionally declaring a class inside a function that you should not do).

The scope of PHP (including OOP) is very simple:

  • variables have scope
  • object properties are available if you have an object reference
  • visibility of object properties may be limited

The only real scope you have is a scope for variables:

 $a = 'foo'; function bar() { $a = 'bar'; } 

Two $a are not completely interconnected in different areas. So simple.

 class Foo { public $a = 'foo'; public function bar() { $this->a; // foo $a = 'bar'; } } $foo = new Foo; $foo->a; // foo 

The property of an object has no scope. You can access it if you have an object in scope. Above, $foo is an object. It is in scope, its property a is public , so it can be accessed using $foo->a . Inside the class, the property is available through $this->a .

$a = 'bar' is a local variable in the function and has nothing to do with $this->a . It is not available anywhere except for the function. See Rule No. 1, scope.

 class Bar { protected $b = 'bar'; public function baz() { $this->b; // bar } } $bar = new Bar; $bar->b; // doesn't work 

If visibility is not public , the property (here b ) is not accessible from outside the class itself. Inside the class, you can access it using $this->b , but not externally using $bar->b . This is not about scope, but visibility.

And these are pretty much domain rules in PHP.

+2
source

You should not embed such classes. It doesn't even have to be. First I suggest running the code. There are several online tools for testing small pieces of PHP code, such as this .

To run the code, as you might expect, it should look like this:

 class second{ public function widgets(){ $a_variable = $a_value; } } class first{ public $second; public function __construct() { $this->second = new second; } } $first = new first; 

A variable starting with $ [az] + is local to the function. A property starting with $ this โ†’ [az] + (where [az] is 1 or more letters) is part of the object.

There is documentation on php.net, which describes the specifics of objects in php here .

If I initialize $ a_variable as $ a_variable, it is only available inside the function, right?

Yes, right. It starts with $ [az]. Not entirely true if you use the global , but that is discouraged.

If I initialize $ a_varialbe as $ this-> a_variable, it is only available inside the second class, right?

Yes, but first you have to announce it. You can do this with public $variable , protected $variable or private $variable . public means that the property can be accessed from the outside, while private means that only the class itself can access it. protected is private external, but public for classes that extend from your class.

( public / private / public became available in PHP 5. In PHP 4, you would use var $variable , which will be public in PHP 5 by default)

Is it possible to initialize $ a_variable as $ first-> second-> a_variable?

You can arbitrarily initialize class variables without declaring them, but this is not what you should be doing.

If so, how can I call it at # 1, # 2 and # 3?

You cannot call the code there (in your example). The code must be inside a function or in a global context (outside the class definition).

+2
source

Brief Explanation of Classes

 class foo{ // This can be accessed anywhere public $i_am_public; // This can be accessed within this class and any sub classes protected $i_am_protected; // Thi can only be accessed within this class private $i_am_private; // This function can be accessed anywhere public function foo_function(){ // This variable is only available in this function // it cannot be accessed anywhere else $variable = 'Hello World'; // However, you can access any of the other variables listed above // like so $this->i_am_public = 'public'; $this->i_am_protected = 'protected'; $this->i_am_private = 'private'; // Write these variables echo $this->i_am_public; echo $this->i_am_protected; echo $this->i_am_private; } } $foo = new foo; $foo->foo_function(); // You can change any publicly defined variables outside // of the class instance like so $foo->i_am_public = 'testing'; 

Specific Answers to Questions

Before moving on, I highly recommend that you do not define a class inside a class! Use class extensions instead, which I will explain later. In fact, I am surprised that your code even works!

If I initialize $ a_variable as $ a_variable, it is only available inside the function, right?

Yes, this will only be available inside the function. If you want to access it outside the function, you need to define it outside the function using one of the definitions of the public, protected, private .

If I initialize $ a_varialbe as $ this-> a_variable, is it only accessible by the inner class second, correct?

It depends on what area you give it to, but you still should not define a class inside the class.

Is it possible to initialize $ a_variable as $ first-> second-> a_variable? If so, what would I call it in # 1, # 2 and # 3?

I cannot answer this since I have never nested a class in a class, once again I would strongly recommend that you change this structure.

Is it possible to initialize $ a_varialbe as $ this-> second-> a_variable? If so, what would I call it in # 1, # 2 and # 3?

See above answer :-)

Attachment Classes

As already mentioned, I have never seen this before, and I am surprised that it even works. You must definitely change this structure.

One suggestion would be to use these extensions:

 class foo{ // This can be accessed anywhere public $i_am_public; // This can be accessed within this class and any sub classes protected $i_am_protected; // Thi can only be accessed within this class private $i_am_private; public function foo_function(){ echo 'Hello World'; } } class bar extends foo { // This is a public function public function baz(){ // These will work $this->i_am_public = 'public'; $this->i_am_protected = 'protected'; // This will not work as it is only available to // the parent class $this->i_am_private = 'private'; } } // This will create a new instance of bar which is an // extension of foo. All public variables and functions // in both classes will work $bar = new bar; // This will work because it is public and it is inherited // from the parent class $bar->foo_function(); // This will work because it is public $bar->baz(); 
+2
source

First of all, your example is the wrong code. PHP does not support nested classes, which means a class inside a class.

if you define a class, the variable initialized inside the method is local to this method, whereas you can โ€œinitializeโ€ the attribute with $ this-> newattribute, you should have declared it and its visibility earlier (before writing the public / private method / protected $ varname = "initial value";).

Your questions 3 and 4 will make sense in a different context, this is when an object is passed as a member of another object, see the example below.

If you build it like

 <?php class First { public $second; } class Second { public $a_variable; } $foo = new First(); $foo->second = new Second(); 

you can access it:

 $foo->second->a_variable = "foo"; 

or inside the method in the second with

 $this->a_variable; 

or inside the method, first with

 $this->second->a_variable; 
+2
source

Source: https://habr.com/ru/post/1447156/


All Articles