Calling one class within another

Asked

Viewed 72 times

0

I am using classes within classes and wanted to know if I can instantiate the second class in the constructor of the first. It works and I didn’t notice any problems related to performance, but I wanted to make sure it’s right. Ex.:

class A{}

I’ll call her in class B

use A

class B{
public $classeA;

function __construct(){
$this->$classeA = new A();
} 
}

1 answer

0


You are not using class within class. See what is a class.

You’re creating a class that has a field and in building it is initiating this field with an instance of another class, that’s all. Only methods are called, classes cannot be called. Understand the correct concept to continue.

Perfectly normal and common to do this.

As mechanism demonstration is all ok, the example is good because it does not expose other details not needed to this problem. But understand that a real class cannot be like that. People want to object-oriented programming without understanding what this means and need to think about a huge amount of things, and almost always do it wrong, starting by using a paradigm to manage complexity in a language of script. So that’s the least of the problems that can go wrong there. The most serious mistakes you will make is not in instantiation, it is in the general formulation of the class.

  • OK, thanks for the reply!! in fact I believe I have formulated the question badly...I want to use methods of other classes within a new class...even so, I’ll take a look at the link yes!! thanks again!!!

  • I can only answer what you asked.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.