Board index   FAQ   Search  
Register  Login
Board index PHP PHP General

basic oop question

General discussions related to php

Moderators: macek, egami, gesf

basic oop question

Postby 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

Postby 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

Postby Thompson » Fri Aug 24, 2012 9:48 pm

I have made some comments in the code. There were a few things that weren't quite right.

Let me know if you have any questions.

Code: Select all

<?php

class class0
{
   private $config;
   
   /*** You are overwriting the paramater your passing to an empty string ***/
   //public function __construct($c = '')
     
   public function __construct($c)
   {
      $this->config = $c; //use_config_var() returns empty string
           
      /*** Your Changing the variable directly after passing a variable? ***/
      //$this->config = 'aaa'; //use_config_var() returns 'aaa'
   }
   
   public function setConfig($conf)
   {
      return $this->config = $conf;
   }
   
   public function getConfig()
   {
      return $this->config;
   }

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

// This is passed to the Constuctor
$obj = new class0('This is some test data');

// Display the config variable using the accessor
echo $obj->getConfig().'<br />';

// Set the config variable using the
$obj->setConfig('This is some test data being set through the mutator');

// Display the config variable using the accessor
echo $obj->getConfig().'<br />';

// Executes the use_config_var method
$obj->use_config_var();

?>

Thompson
New php-forum User
New php-forum User
 
Posts: 2
Joined: Wed May 02, 2012 4:20 am
Location: Sydney, Australia

Re: basic oop question

Postby 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?
faust
New php-forum User
New php-forum User
 
Posts: 109
Joined: Thu May 03, 2012 7:22 am

Re: basic oop question

Postby 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.
NigelRen
php-forum Active User
php-forum Active User
 
Posts: 450
Joined: Fri Aug 05, 2011 9:53 am

Re: basic oop question

Postby sivakami » Wed Sep 05, 2012 12:01 am

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


Return to PHP General

Who is online

Users browsing this forum: No registered users and 0 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.