Jump to content

Recommended Posts

Suppose you have a class. In this class you might need to create some public properties based on informaiton retrieved from a database. In any case, you won't be able to declare them on beforehand, as you don't know which properties are needed.

 

Simple example:

 

<?php
class foo {
function foo() {
	$this->bar = "Hello, World!";
}
}
$foo = new foo();
echo $foo->bar;
?>

 

Is this considered bad practice? What else could be done? I suppose you could stuff the retrieved information into an array, but I would rather keep it simple looking. Like, $foo->bar, rather than $foo->data['bar'].

Link to comment
https://forums.phpfreaks.com/topic/100286-bad-practice/
Share on other sites

You could use the __set and __get magic methods, or create a stdClass and assign it to one variable.

 

<?php

class foo {

    private $_data;

    public function __contruct(){
        // TODO: class contructor function
    }

    public function __get($key){
        return $this->_data[$key];
    }

    public function __set($key, $value){
        $this->_data[$key] = $value;
    }

}
?>

 

You can then access data from the class like this:

<?php
$class = new foo();

$class->test = 'Test Variable';
// performs $this->_data['test'] = 'Test Variable';

echo $class->test;
// displays 'Test Variable'
?>

 

stdClass is basically just an alias to an array:

<?php

$class = new stdClass();
$class->test = 'Test Variable';
// performs $class['test'] = 'Test Variable';

echo $class->test;
// displays 'Test Variable'
?>

Of course with stdClass you can't define functions, as its just a special type of array.

 

 

.Cobby

Link to comment
https://forums.phpfreaks.com/topic/100286-bad-practice/#findComment-512827
Share on other sites

Well, the example I gave works, but just because something works doesn't mean it's a good idea. What is generally thought of overloading? As in, is it considered okay or a bad thing to do?

Overloading is something which is quite commonly used. Take for example the + operator in some languages. In some languages if it's used with strings it will concatenate them, but if it's used with e.g. two integer values then they are added to each other. That's an example of overloading polymorphism.

 

I'd also agree that using the magic __get() and __set() methods would be better than dynamically creating class members.

Link to comment
https://forums.phpfreaks.com/topic/100286-bad-practice/#findComment-512909
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.