Tuesday 15 April 2014

php - Variables set in the constructor are forgotten? -



php - Variables set in the constructor are forgotten? -

what i'm trying here first create variable $sql_info , set default value, constructor takes 1 argument, set $sql_info variable. if echo $sql_info; within constructor works fine, if seek in different function, doesn't echo screen, not default value. why that?

<?php class connection { private $sql_info = "default"; function __construct($info) { //set new value of $sql_info $sql_info = $info; } function connect() { global $sql_info; //echo newly set string echo $sql_info; } } ?>

thanks!

you need write $this!

try this:

<?php class connection { private $sql_info = "default"; function __construct($info) { //set new value of $sql_info $this->sql_info = $info; } function connect() { global $sql_info; //echo newly set string echo $this->sql_info; } } ?>

php oop constructor

No comments:

Post a Comment