0

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?

DaveHolt
  • 119
  • 1
  • 3
  • 13
  • You are used to use to create a prototype object. You are not used to work in an object oriented pattern from a class definition. Define each class on its own, keep each of them in their own file. Don't try to have it all in one big place. Also, read upon dependency injection. – k0pernikus Feb 21 '17 at 11:36
  • And avoid using `stdClass`. Define your own classes. – k0pernikus Feb 21 '17 at 11:37

2 Answers2

0

In PHP you should use namespaces and classes to organize your code.

Namespaces: http://php.net/manual/en/language.namespaces.php

\some\name\space\class::staticMethod()

Classes & Objects: http://php.net/manual/en/language.oop5.php

$obj = \some\name\space\class();
$obj->someMethod();
ponury-kostek
  • 7,824
  • 4
  • 23
  • 31
  • Hi. I looked into namespaces but got an error when I tried to nest them like so: `namespace app{namespace {database}}`. I was hoping this would turn into `\app\database::connect()` or similar – DaveHolt Feb 21 '17 at 11:36
  • http://php.net/manual/en/language.namespaces.basics.php in `file2.php` example is exactly what you are looking for. Have you read this http://php.net/manual/en/language.namespaces.nested.php ? – ponury-kostek Feb 21 '17 at 13:02
  • OK so maybe namespaces are the way forward here but they are frustrating. I just can't visualize them. Is there a way to define them like in my tree structures above? Or at least define them the standard way but **view** classes/namespaces as the tree structure? All I care about is being able to see where the code fits in to the project. Using `var_dump` on a class unfortunately doesn't show any "children" so to speak. – DaveHolt Feb 21 '17 at 13:51
-1

Unfortunately, all the tutorials out there don't show how to nest multiple objects like my javascript example above.

It's because you cannot define nested objects in PHP.

However, you can extend a class any number of times, you can use namespaces, and if you need multiple inheritance, Traits lets you to do that.

Also, method chaining is also plausibe by returning the object instance at the end if the class methods. More info here: How to create a fluent query interface?

Community
  • 1
  • 1
Gergely Lukacsy
  • 2,881
  • 2
  • 23
  • 28
  • Traits do look good actually. I just made another comment on the other answer about visualization. I guess I want to see the code in a tree structure. If I went down this route, would there be a way to get PHP to spit out the logical structure of all these objects/relationships so I could view it in tree format? – DaveHolt Feb 21 '17 at 13:53