简体   繁体   中英

How does extends work in classes?

I've been trying to build a PDO extension, and I wanted to have special classes in different files but I wanted to have them all link to the same original class.

I have worked with some Frameworks and I see that they use the extends class keyword, and I thought that it added the class you are making to the class that you have given.

Some code I have tried is:

class PSMQuery extends PSM {

   // Functions and Jargon

}

I tried making an object for the original PSM class:

$psm = new PSM(/*Information*/);

But when I call the $psm variable like $psm->functionInTheExtendedClass it comes up with an error saying that it was an undefined method when I called it.

Am I using the extends keyword incorrectly?

Am I using the extends keyword incorrectly?

You use it correctly, but it works the other way around.

If PSMQuery extends from PSM , this means you can access and use stuff from PSM in PSMQuery , but not the other way around.

I think to understand it you can use a good example:

class twoRoomApartment extends building { }

So now you can think logical and already see, that a two room apartment probably extends from a building and not the other way around.

Means now in your code, you just create an instance of PSMQuery .

It's inheritance.

Parent : PSM Child : PSMQuery

When you use extends you are extending parent class functionality and creating child class.

Your child class will inherit all the parent class functionality.

Parent will not get child class functionality.

So when ur trying to create object of parent class it doesn't know child class functionality.

You need to create object of child class and then you can access methods from parent class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM