Solved

sub/child functions

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

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 Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP Installer 5 37
How do I fix this UPDATE error? 7 32
Need to remove %20 from url in get 17 36
Ahax pagination 9 32
This article discusses four methods for overlaying images in a container on a web page
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

861 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