Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

sub/child functions

Posted on 2014-09-03
2
Medium Priority
?
123 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 17

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 111

Accepted Solution

by:
Ray Paseur earned 2000 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

916 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