Jump to content

Notice: Undefined Variable & Call Stack


WillUK
 Share

Recommended Posts

Hi

 

I am buidling a Blog System using OO techniques (or at least attempting to)..

 

I keep getting an error :

 

Notice: Undefined variable: blog_categoryName in C:\wamp\www\atkinsonsCMS\admin170976\blog_categories.php on line 13

...And then underneath all that is is displaying 'Call Stack'. I've never come across this notification before so I guess it is an OO term, and the a list of unexpected outputs: #, time, memory, function and location....each with an associated value.

I attempted to resolve the variable issue in the usual way (as you can see in my code), but still the errors/warnings persist.

 

The purpose of this part of the code is to simply retrieve SQL data (which is a list of blog categories), allowing the user to select one and then click through to be directed to the appropriate category page (although this last requirement of the code has not yet been programmed).

 

Could those who have experience of this please take a look at my code and let me know where I am going wrong?

 

Thanks

 

blog_categories.php

This file calls on the the blogFunctions.php Category Class, and the displays the results.

session_start();
$pageTitle = "Blog Categories";
include("../includes/admin_header.php");
include("../classes/blogFunctions.php");
echo '<div class="admin_main_body"><br /><br />';
echo "<h2>Category List</h2><br />";
$content = new Category;
$content -> process();
//print_r(debug_backtrace());
echo "<a href=blog.php>$blog_categoryName</a>";
echo '</div>';
echo '</div>';
include("../includes/admin_footer.php");

 

 

 

blogFunctions.php

This class deals with the retreaval of the SQL data, and then passes it to blog_categories.php

class Category {
               public $errors;
               public $blog_categoryName = NULL;
public function __construct() {
                $this->errors = array();
                $blog_categoryName = $this->blog_categoryName;
                }                       
public function getCategories(){
                if(!isset($blog_categoryName))
                {
                $blog_categoryName = 'value';
                }
                ($GLOBALS["___mysqli_ston"] = mysqli_connect("localhost", "atkinson", "XYZ111WA")) or die(((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)));
                ((bool)mysqli_query($GLOBALS["___mysqli_ston"], "USE atkinsonscms")) or die (((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)));
                mysqli_query($GLOBALS["___mysqli_ston"], "SELECT * FROM blog_category ORDER BY ASC");
                $result = @mysqli_query($GLOBALS["___mysqli_ston"], $query);
                if ($result) {
                while ($row = $result->fetch_object()) {
                $blog_categoryName = $row->blog_categoryName;
                 }
                //need to use mysqli query to retrieve results. 
                if(mysqli_affected_rows($GLOBALS["___mysqli_ston"])< 1)
                $this->errors[] = 'Could not retrieve data';
                }
                                }
public function show_errors(){
                echo "<h3>Errors</h3>";
                foreach($this->errors as $key=>$value)
                echo $value."<br>";
                                    }
public function process() {
                $this->getCategories();
                return count($this->errors)? 0 : 1;
                                    }
} //Closes Category Class.            

 

Link to comment
Share on other sites

Thanks for that but I don't get why?!!

 

I've put the line:

public $blog_categoryName = NULL;

 

This has been put at the top the class, before any methods have been created.

 

I've also put the line just to make sure, by giving the variable a value:

 

if(!isset($blog_categoryName))
                {
                $blog_categoryName = 'value';
                }

 

So I have set the variable haven't I??

Link to comment
Share on other sites

LOL, and since the places where you are defining and setting $blog_categoryName aren't written correctly to reference or set the main program's $blog_categoryName variable, your code needs some help.

 

Sorry to be blunt, but if you are writing a major application in php, you cannot just take a block of code and put a function definition definition around it and call it a function. You must define what input data values the function will receive, if any, define what processing and result the function will produce, and what value(s) the function will return, if any.

 

Functions and OOP class methods have their own isolated variable scope so that you can write whatever code and use whatever variables you need inside the function/method to accomplish the stated goal of that function. Also, OOP classes have their own isolated variable/property scope so that you can define and use whatever class variables/properties you need inside of that class to accomplish the stated goal of that class.

 

You need pass data values into the function/method as  parameter(s) when you call that function/method or you need to use class variables/properties to supply the data values and you need to either set class variables/properties with the value(s) that your function/method produces or return the value(s) to the code that called the function/method.

Link to comment
Share on other sites

Specifically for your Reply #2 post questions -

 

public $blog_categoryName = NULL;

 

^^^ That defines $blog_categoryName as a public variable/property of your Category class. After you create an instance of your class using - $content = new Category;, then you can reference the class variable/property using - $content->blog_categoryName in the scope of your program where $content exists.

 

To set or reference the public $blog_categoryName variable/property from inside your class code, you use $this->blog_categoryName

 

Using just $blog_categoryName inside your class code is creating a local variable that only exists within the class method where it is referenced or set. If you used $blog_categoryName in every one of your class methods - A) Each class method would have its own separate local $blog_categoryName variable, and B) Since local variables in functions and class methods are destroyed when that function/class method ends, it doesn't exist longer then the time it takes for the code in that function/class method to run.

Link to comment
Share on other sites

Thanks. Your answer is a bit advanced for me, but I'll try to get my head around it.

For your information, I am quite new to object oriented programming and am using a format found within a tutorial I referred to. If it isn't any good, don't blame me. I am just learning.

But thanks for your advice :)

 

 

Link to comment
Share on other sites

This thread is more than a year old.

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.

 Share

×
×
  • 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.