Coming from javascript, I am used to having all of my functions nested within objects like so:
var app = {
something: {
something: {
some_variable:"something",
something: function() {
return this.some_variable;
}
}
}
};
console.log(app.something.something.something()); // "something"
This serves as a good way to keep all the functions categorized and named logically (for me).
In PHP, I have been doing this using arrays:
$app=[
"name"=>"my_app",
"database"=>[
"password"=>"497624779",
"connect"=>function(){
//connect to db here using $GLOBALS["app"]["database"]["password"]
},
"update"=>function(){
//update records, etc
}
],
"page"=>[
"data"=>[
"page_1"=>[
"title"=>"Home",
"icon"=>"",
"etc"=>""
],
"page_2"=>[
"title"=>"",
"icon"=>"",
"etc"=>""
],
"page_3"=>[
"title"=>"",
"icon"=>"",
"etc"=>""
]
],
"loading"=>function($page_name){
return $GLOBALS["app"]["page"]["data"][$page_name]["title"]." is loading...";
}
]
];
It means I can do this:
$app["database"]["connect"]();
echo $app["page"]["loading"]("page_1"); // "Home is loading..."
How would I migrate this to using objects so that I can do this:
$app->database->connect(); // connect function would use $this->password, not GLOBALS
$app->page->loading("page_1");
Unfortunately, all the tutorials out there don't show how to nest multiple objects like my javascript example above. I like the tree-like structure of the arrays for visualization but I can't find a way to define an object this way - only examples like this:
$app=new stdClass();
$app->database=new stdClass();
$app->database->password="497624779";
//etc
i.e., not very visual at all. If I refactor all my code this way, I will get lost in it, unless I do a var_dump of the object periodically.
Vague question, I know. Can anyone point me in the right direction?