Returning specific information with ->ID, ->Account, ->Mail, ->Skype etc...
(With one main function like User(), but without user class)
I don't really have idea what is smart way to create bunch of information picked up with the "arrow" I actually know what are they, but I don't know how to explain or what ever it is that I can search it from google.
But I want it to be like this:
function User($Name) {
// Other shiiieeeeet
}
Example results:
<?php $User($_SESSION['account'])->ID; ?>
<?php $User($_SESSION['account'])->Account; ?>
<?php $User($_SESSION['account'])->Mail; ?>
<?php $User($_SESSION['account'])->Skype; ?>
I can use such a feature for logged in users as I can use it for profiles like this.
<?php $User($_GET['profile'])->ID; ?>
<?php $User($_GET['profile'])->Account; ?>
etc...
But I don't have a clue how do I create "inner function" / "nested function" or whatever that I can use it with arrow. I've seen such a thing before, but it doesn't really help me just now. If I create functions inside of function I cannot use them with arrow.
Any ideas? Thanks! :)
Re: Returning specific information with ->ID, ->Account, ->Mail, ->Skype etc...
thats mvc programming and theres a bit different between php (mvc) and php it self , if you wanna code the mvc way u use "->" , i recommend you search for php mvc programming it can help you
Re: Returning specific information with ->ID, ->Account, ->Mail, ->Skype etc...
How about creating a class that will accept the ID via its constructor and you could use this object later by calling it's properties/functions.
PHP Code:
class User
{
public $ID;
public $Account;
public $Mail;
public $Skype;
public function __construct($id)
{
fetchUser($id);
}
public function fetchUser($id)
{
$this->ID = $id;
//do some DB fetching and assign the values from the DB to the class's data members. I am assuming a variable $row is containing the datarow of the query you executed on DB.
$this->Account = $row['Account'];
$this->Mail = $row['Mail'];
$this->Skype = $row['Skype'];
}
}
// ---- Usage
$obj = new User(123); //create the Class's object passing the ID to the constructor
echo $obj->ID;
echo $obj->Account;
echo $obj->Mail;
echo $obj->Skype;
// ---- If you want to fetch another user ID's data:
$obj->fetchUser(3564);
echo $obj->ID;
echo $obj->Account;
echo $obj->Mail;
echo $obj->Skype;
This is a simple demonstration only. You have to improve it and take care of the rest of the things!
:wave:
Re: Returning specific information with ->ID, ->Account, ->Mail, ->Skype etc...
I'm not sure exactly what you're looking for.
Perhaps it's the 'magic' function __get, which lets emulate members without explicitly defining them:
PHP Code:
<?php
class User
{
private $dict;
private function __construct(array $props) { $this->dict = $props; }
public function __get($key) { return $this->dict[$key]; }
public static function lookup($name) { return new User(['name' => $name]); }
}
$u = User::lookup('John Citizen')->name . "\n";
?>
I'm not sure why you'd want to do that, to be honest.