Solved

sub/child functions

Posted on 2014-09-03
2
110 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses four methods for overlaying images in a container on a web page
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 count occurrences of each item in an array.

708 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

13 Experts available now in Live!

Get 1:1 Help Now