Solved

sub/child functions

Posted on 2014-09-03
2
111 Views
Last Modified: 2014-09-13
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.
0
Comment
Question by:tonelm54
2 Comments
 
LVL 16

Expert Comment

by:Chris Harte
ID: 40303209
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
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 40303544
What MunterMan said.  PHP is a single-inheritance language with a robust object-oriented programming model.  Here's an example of a simple "car" class (Check the references in the code).
http://www.iconoun.com/demo/temp_tonelm54.php

Two good articles here at EE:
http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/A_2626-Beginning-Object-Oriented-Programming-in-PHP.html
http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/A_2631-Advanced-Object-Oriented-Programming-in-PHP.html
<?php // demo/temp_tonelm54.php
error_reporting(E_ALL);
echo '<pre>';

// SEE http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28511325.html
// REF http://www.phptherightway.com/
// REF http://www.php.net/manual/en/language.oop5.php
// REF http://www.php.net/traits

class Car
{
    // THE CLASS CONSTRUCTOR SETS UP SOME DEFAULT INITIALIZATION VALUES
    public function __construct($make="Ford")
    {
        $this->car = $make;
        $this->deg = 0;
        $this->dir = 'straight';
    }

    // THE DESTRUCTOR TAKES CARE OF CLEANUP, AS NEEDED
    public function __destruct()
    {
        echo PHP_EOL . $this->car . ' CRASHED!';
    }

    // THE DRIVE() METHOD ADJUSTS DIRECTION BY DEGREES
    public function drive($dir, $deg)
    {
        if ($dir == 'left')
        {
            $this->deg = $this->deg - $deg;
        }
        else
        {
            $this->deg = $this->deg + $deg;
        }
        $this->deg = $this->deg % 360;

        if ($this->deg  > 0) $this->dir = 'right';
        if ($this->deg  < 0) $this->dir = 'left';
        if ($this->deg == 0) $this->dir = 'straight';
    }

    // THE STATUS() METHOD FUNCTION RETURNS THE CURRENT DIRECTIONAL INFORMATION
    public function status()
    {
        return PHP_EOL
        . $this->car
        . ' IS DRIVING '
        . $this->dir
        . ' AT '
        . $this->deg
        . '&deg;'
        ;
    }
}

// CREATE A NEW OBJECT -- INSTANCE OF A CAR
$bimmer = new Car('BMW');

// CHECK THE INITIAL OBJECT STATUS
echo $bimmer->status();

// MAKE SOME DRIVING DECISIONS, CHECKING EACH AS WE GO
$bimmer->drive('left', 45);
echo $bimmer->status();

$bimmer->drive('right', 45);
echo $bimmer->status();

$bimmer->drive('left', 365);
echo $bimmer->status();

$bimmer->drive('right', 45);
echo $bimmer->status();

// INVOKES THE CLASS DESTRUCTOR
unset($bimmer);

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now