basic oop question

General discussions related to php

Moderators: macek, egami, gesf

Post Reply
faust
New php-forum User
New php-forum User
Posts: 109
Joined: Thu May 03, 2012 7:22 am

basic oop question

Post by faust » Fri Aug 24, 2012 1:02 pm

I'm really embarassed to ask this but I just cant figure it out... I have a class:

Code: Select all

class class0
{
        private config;

	public function setConfig($conf)
	{
		return $this->config = $conf;
	}
	
	public function getConfig()
	{
		return $this->config;
	}

        public function use_config_var()
        {
                echo $this->config;
        }
}

$obj = new class0;
$obj->setConfig('a');
echo $obj->getConfig(); //echoes 'a'
$obj->use_config_var(); //echoes empty string ''
How can I use the $config variable inside the class that it is defined in?

faust
New php-forum User
New php-forum User
Posts: 109
Joined: Thu May 03, 2012 7:22 am

Re: basic oop question

Post by faust » Fri Aug 24, 2012 1:06 pm

I forgot to mention that the weirdest thing is that when I tried using __construct to set the variable the following happens:

Code: Select all

class class0
{
     private $config;

     public function __construct($c = '')
     {
             $this->config = $c; //use_config_var() returns empty string
             $this->config = 'aaa'; //use_config_var() returns 'aaa' 
     }

      //everything else is like in the previous post
}

$obj = new class0('bbb'); //this returns and empty string

faust
New php-forum User
New php-forum User
Posts: 109
Joined: Thu May 03, 2012 7:22 am

Re: basic oop question

Post by faust » Fri Aug 24, 2012 11:54 pm

Hi and thanks for the help. I think I haven't explained my problem right. Anyway, it turned out that in my case, this wasn't the problem at all. I know everything you said, but I was mistaken at something else. An btw, isn't this valid

Code: Select all

public function __construct($c = '')
I think this is not overriding the value I pass, this is a default value in case I don't pass a value http://www.php.net/manual/en/functions.arguments.php

However, my problem is something else. I'm have to get a class name from a config file (array) and create a new object from the returned string. Everything's ok, I get the value and return a string. But when I create the object:

Code: Select all

$model = Config::get($conf_file);
$a = new $model;
return $a;
It returns an error: "Class name must be a valid object or a string". I am absolutely sure that the $conf_file var is pointing to the right file, because the $model var contains the right class name. If I put a static value like:

Code: Select all

$model = "classname";
everithing works ok. Any idea what am I doing wrong?

NigelRen
php-forum Fan User
php-forum Fan User
Posts: 595
Joined: Fri Aug 05, 2011 9:53 am

Re: basic oop question

Post by NigelRen » Sat Aug 25, 2012 12:05 am

A couple of things...
In your first example the $ sign is missing of private config; which causes an error. Once corrected it should work as you'd expect.

The second example works OK - I'm assuming that your trying to echo $obj though - which is the object, not the value of the config field. You could always use print_r($obj); to see what the object is made up of.

sivakami
New php-forum User
New php-forum User
Posts: 17
Joined: Tue Sep 04, 2012 11:26 pm

Re: basic oop question

Post by sivakami » Wed Sep 05, 2012 12:01 am

object,class

Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests