Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Starting up the application reusing objects

Featured Replies

Sorry to post two topics in one day. I've been trying to get to grips with how to struture a very basic application. I've been trying to separate logic from the view and minimize global variables, like I keep reading you should do. The question I have about is the startup of an application.

 

.<?php
include('model.php');
$secretConfessions = new SecretConfessions();


class SecretConfessions {
    private $model;
    
    function __construct() {
        $this->model = new Model();
        $this->go();
    }
    
    function go() {

        if (isset($_POST['add_confession']) && isset($_POST['the_confession'])) {
            $this->model->addConfession($_POST['the_confession']);
            header('Location: secretconfessions.php');
            exit();
        }

       
        $confessions = $this->model->getConfessions();
        $this->renderView($confessions);
    }
    
    function renderView($confessions) {
        include("view.php");
    }
}
 

 

This will probably end up being my index page. Now at the top of the page I create a SecretConfessions object which I'm trying to make act as a controller, This contains the model (it won't always be called model).

 

At the moment every time the user adds a confession (just an anonymous post) they will be redirected back here. But that will create another SecretConfessions object which will create another model object. How do I start of an application like this so it just creates the objects it needs once, without it doing it all again when the form is submitted.

 

How would a website like twitter (but much much simpler) start up? Does it keep variables the whole time the user is adding posts?

 

 

 

The concept you have chosen to work with is correct, but the class file should be in the background. Your index page needs to deal with providing the output to the user and passing data back to your class. Using objects in PHP is fairly straight forward once you have grasped how they are used.

 

Explaining that here would be very hard, however; There is a tutorial by Matt Doyle on elated.com that I like. It will explain this and several other processes very clearly. Just remember that he does explain he uses some 'dirty tricks' and short cuts, but the principles is what you need first and thus this is a good starting point to build on. Try and work through all 4 parts of the tutorial, it will be quite enlightening for you.

 

There is one more point I would like to make though. When I was at the same stage you are at now nobody told me that learning how to use object's in PHP first is the hard way to do things. I would recommend that you try learning the basics of JavaScript so that you can fully understand what an object is, what you can do with it, and how to make use of it. Once you have those principles clear in your own mind PHP and 'OOP PHP', as it is often called when using objects, will be a lot more logical and easy to learn.

  • Author

Thank you I will check out that tutorial. I'm not sure what you mean by class file should be in the background? Can you clarify please?

 

I already have quite a bit of experience with javascript. I've made serveral quite complex games javascript, and I have made games in java. So I'm used to making objects. It's more of how to specifically do it in a website that's giving me trouble. I'm only just started learning php though.

 

Thanks again for the link.

Thank you I will check out that tutorial. I'm not sure what you mean by class file should be in the background? Can you clarify please?

 

I already have quite a bit of experience with javascript. I've made serveral quite complex games javascript, and I have made games in java. So I'm used to making objects. It's more of how to specifically do it in a website that's giving me trouble. I'm only just started learning php though.

 

Thanks again for the link.

The tutorial will explain all, but basically your app is built in layers. The top layer is the output, then there is the logic, and finally the data layer.

 

It seems you have a definite advantage over me before you start with PHP then, so I am sure you will soon be building every app you desire.

Edited by Weedy101

  • Author

This is what I was trying and failing express http://stackoverflow.com/questions/1023282/maintain-object-state-with-different-pages-using-php .And answers my question. It was confusing me about keeping objects alive, but I see you pretty much just don't. That's gong to take some getting used to.

 

But that tutorial, Weedy101, is awesome and very clear. Thanks again.

  • Author

I'm just learning the basics of php and mysql at the moment. But after I've made a few small websites to practice I think I will go with code igniter, as it seemed like the most straight forward.

 

People always throw that reinvent the wheel phrase around to newbies who are trying to understand what the wheel actually is.

  • Author

I looked at the inroduction on the Silex page. There so much I didn't understand I couldn't possibly use a framework like that yet. It already assumes you should know far more than I know.

  • Author

That's kind of the same thing I went through making games. When I started off I wanted to write every line of code myself, and everyone was saying use a framework, but I found them intimidating. My last couple of games I started using a framework, and I wished I'd tried it sooner. It actually taught me a lot more about how to structure my code and it wasn't that bad when you just dived in. It was well documented though.

 

Shame about code igniter, it had such clear instructions and tutorials.

 

 

I do understnd the bascs of oop, but just not in regards to websites yet. I'm dedicating an hour and a half a day to going through code examples in books, and tutorials, and another hour and half making simple websites. I can't believe how slow I am at making a websites. So much I have to learn. I have an idea of a very complex website. That's why I've started in the first place, but now I'm realizing It will probably be about a year before I start it.

  • Author

It definitely has clearer instructions than the other one. I need to check to see if it's compatible with my hosting company, ipage. If it is I'll give it a go. I've been looking at Yii, that looks pretty clear too.

Edited by Dream Of Sleeping

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.