A
A
AlexRas2015-12-03 11:09:52
PHP
AlexRas, 2015-12-03 11:09:52

How to make a function in php in modx or something else?

Hello, help me figure out how the functions in php work, or maybe something else is needed here.
There is a code:

if (true) {
  $bd_user = $modx->newObject('User');
  $bd_user->set('fullname', $_POST['name']);
  $bd_user->set('email', $_POST['email']);
  $bd_user->save();
} else {
  $id = $_SESSION['id'];
  $bd_user = $modx->getObject('User',array('id'=>$id));
  $bd_user->set('fullname', $_POST['name']);
  $bd_user->set('email', $_POST['email']);
  $bd_user->save();
}

This is a sample code. There is a repeating part in the code, tell me how and where you can stuff this repeating code so that it is only in one place.
I tried functions, but something does not work. Gives an error message
Fatal error: Cannot redeclare

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
AlexRas, 2016-01-18
@AlexRas

Created a class helped

D
Dmitry Kravchenko, 2015-12-03
@mydearfriend

create a snippet, of course
Cannot redeclare says that a function with that name already exists

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question