-3

I tried to make a function inside a function:

<?php
class usermanager extends datamanager {

public $id;
public $name;
public $from;
public $twitter;
public $instagram;
public $skype;

public $regIP;
public $lastIP;
public $email;

public function __construct($exists = false,$uid = 0) {

if ($exists == true) {
$this->id = $uid;
$this->name = $this->fetch("SELECT * FROM users WHERE ID = '".$uid."';")->name;

public function getProfile() {
profile();
}

}

else {

public function new($name,$password,$email) {

$this->autocommit(false);
if (!($do = $this->query("INSERT INTO users (name,password,email,rank) VALUES ('".$name."',PASSWORD('".$password."'),'".$email."','0');"))) {
$this->rollback();
return false;
}
else {
$this->commit();
return true;
}

} //end new()

} //end else

} //end __construct()

public function __set() {

trigger_error("Can not edit read-only variable",E_USER_ERROR);

} //end __set()

private function profile() {

$gets = array("twitter","instagram","skype","from");

$fetch = $this->fetch("SELECT * FROM users WHERE ID = '".$this->id."';");
foreach ($gets as $get) {
$this->$get = $fetch->$get;
}

}

} //end class
?>

Because I saw this I thought it would work, but I got:

Parse error: syntax error, unexpected T_PUBLIC in /home/a7405987/usermanager.php on line 21

Why doesn't this work?


It is fixed now, but now I'm getting another error:

Call to undefined function getProfile()

How can I fix this?

11
  • line 21 which is? public function new? Commented Mar 29, 2016 at 13:52
  • Hmm, yeah, well, functions inside functions is not really a thing that exists in PHP. Commented Mar 29, 2016 at 13:53
  • 1
    @Mr.Llama First sentence of accepted answer: "There is none basically, I've always treated this as a side effect of the parser."Yes, you can write functions inside functions and that actually does something, but it probably doesn't do what OP thinks it does, and it most certainly doesn't work the way OP wants it to work. Commented Mar 29, 2016 at 13:56
  • 1
    new is a reserved keyword in PHP. You can't create a function with that name. Commented Mar 29, 2016 at 13:56
  • 1
    @BjörnSchönrock - Your edit (the new problem about mysqloi) should be a new question. Commented Mar 29, 2016 at 15:18

2 Answers 2

3

Defining a function within a function isn't a great idea. Aside from classes, any function definitions are automatically global. The public and private keywords are only valid in a class definition, not within a class function. If you were to remove the public from your inner function definition, it would run without error, but the result would be a globally defined getProfile().

This example should help demonstate the issue:

<?php

class Test {
    public function foo() {
        function bar() {
            echo "Hello from bar!" . PHP_EOL;
        }

        echo "Hello from foo!" . PHP_EOL;
    }
}

$t = new Test;
// PHP Fatal error:  Call to undefined method Test::bar()
// $t->bar();

// Works, prints "Hello from foo!"
// bar() is now defined, but not where you expect
$t->foo();

// PHP Fatal error:  Call to undefined method Test::bar()
// $t->bar();

// Works, prints "Hello from bar!"
// Note that this is global scope, not from Test
bar();

Demo in action

Sign up to request clarification or add additional context in comments.

Comments

2

You cannot use modifiers public/private/protected inside a member function or here a constructor. You can however declare a function inside a method :

public function classMember() {
    function doSomething() {
    //do something
    }
    doSomething()
}

For your particular problem, you should instanciate your class and then check if it exists, otherwise insert it.

You cannot change the structure of a class depending on the context it is called

11 Comments

If I make a new usermanager() and I want to get an existing user from the database I can use usermanager(true,$id); but the function getProfile() must only exist if I use an existing user.
But my question is, why do I get this error?
The way to do that is to make getProfile throw an exception if it's called without an existing user. You get the error because you're trying to do something that the syntax of the language doesn't allow. It's like trying to speak to an Indian in Chinese. Doesn't work.
then you are not thinking the problem correctly, you should instanciate your class and THEN, insert it or not from the code that uses it.
the answer is "because it is not allowed".
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.