Solved

sub/child functions

Posted on 2014-09-03
2
117 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 110

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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.
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…
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.

623 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