user4815703
user4815703

Reputation:

Understanding Class Properties

I am still in the process of experimenting with PHP classes and I wanted to confirm something before I create bad habits.

When you set a property outside a function...that variable is to be used for multiple functions? What would be the difference If I set a variable inside a class and passed it on like that?

E.g.

class myClass{
    private $variable;

    public function __construct() {
        // I will set the class variable.
    }

    public function use(){
        //I will use the class variable or I will set a variable myself.
    }
}

Have I got the correct idea of how classes properties should be used?

Let's say I retrieve user data from a form and save those values as class properties, can I then use those properties to add the user to DB at a later stage? Should classes have a destruct function? Or can I just call a new class again? Thanks.

Upvotes: 1

Views: 35

Answers (1)

SidOfc
SidOfc

Reputation: 4584

Depending on your situation you could have a class of which only one instance can exist but you will not really have to worry about that yet

(get into it first and try to understand things before diving in without an underlaying reason)

Within a class you decide how you wish to structure your layout. Depending on the kind of structure you personally like and ofcourse using best practises you'll use a typical design pattern and code guidelines to help you out (you can find these everywhere for PHP they are called PSR - google it ;))

What you are unsure about is something very case specific.

If you have a form that creates multiple users at a time (very unlogical but to get the idea) and you wish to submit this to your PHP backend, a logical way to go about it is to create a small class or object with just properties.

this could look something like:

class Person {
    public $name;
    public $surname;
    public $age;
}

What you would be doing here is creating a consistent object that would atleast have these properties to call.

This tiny object is then loadable into your main class for example:

class User {
    public function saveUser(Person $person) {
        // do something to $person->name etc...
    }
}

What you are doing here would be using a function call from an object, inserting another object (the person) as an argument and doing your manipulations there.

This is however not limited. You can for instance, create an array of Person objects and loop them in a database.

This way will allow you to create objects like you want but reduce the overhead of creating an entirely new object.

You would just split them up into a "model" which has the properties defined, and a "controller" in which you manipulate the model, creating, reading, updating and deleting ("CRUD").

It's abit scarse info but when it comes to models I cannot tell you how much there actually is to it to think about so over time you're going to want to do some research on these things to become better at what you want to do ;)

Good luck ;)

Upvotes: 1

Related Questions