Link to home
Start Free TrialLog in
Avatar of tonelm54
tonelm54

asked on

sub/child functions

Is it possible to create sub/child functions in PHP?

For example:-
class car() {
     function drive() {
          function left($deg) {
          }
          function right($deg) {
          }
     }
}

Open in new window


Im trying to stucture my code a bit better and would like to try and group some code in 'sections', and seen in other languages something similar like:-
     car.drive.left(45);

Open in new window


I know in PHP, it would be something like:-
     $myCar = new car();
     $myCar.drive.left(45);

Open in new window


Basically this is more of a question, more than a requiment......

Thanks in advance.
Avatar of Chris Harte
Chris Harte
Flag of United Kingdom of Great Britain and Northern Ireland image

Yes you can, but the point of an object oriented language is you do not have to. Organising your code is what classes are for. I think you may be approaching this from the direction of a procedural coder where as you should be thinking in objects and properties. Go and search for a few php specific oop tutorials, they will give you a clearer idea of what oop can do. Here is a starting point

http://php.net/manual/en/language.oop5.php
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial