Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Fatal error: Cannot access protected property Test::$protected in C:\wamp\www\phpvqp3_scripts\ch05\visibility.php on line 71

Posted on 2014-01-23
6
Medium Priority
?
1,350 Views
Last Modified: 2014-01-25
from oop php tutorial
why / how is this error

Fatal error: Cannot access protected property Test::$protected in C:\wamp\www\phpvqp3_scripts\ch05\visibility.php on line 71

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Visibility</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
<?php # Script 5.4 - visibility.php
//  This page defines and uses the Test and LittleTest classes. 

# ***** CLASSES ***** #

/* Class Test.
 *  The class contains three attributes:
 *  - public $public
 *  - protected $protected
 *  - private $_private
 *  The class defines one method: printVar().
 */
class Test {

    // Declare the attributes:
    public $public = 'public';
    protected $protected = 'protected';
    private $_private = 'private';  

    // Function for printing a variable's value:
    function printVar($var) {
        echo "<p>In Test, \$$var: '{$this->$var}'.</p>";
    }

} // End of Test class.

/* LittleTest class extends Test.
 * LittleTest overrides printVar().
 */
class LittleTest extends Test {
    function printVar($var) {
        echo "<p>In LittleTest, \$$var: '{$this->$var}'.</p>";
    }
} // End of LittleTest class.

# ***** END OF CLASSES ***** #

// Create the objects:
$parent = new Test();
$child = new LittleTest();

// Print the current value of $public:
echo '<h1>Public</h1>';
echo '<h2>Initially...</h2>';
$parent->printVar('public');
$child->printVar('public');
// Also echo $parent->public or echo $child->public.

// Modify $public and reprint:
echo '<h2>Modifying $parent->public...</h2>';
$parent->public = 'modified';
$parent->printVar('public');
$child->printVar('public');

// Print the current value of $protected:
echo '<hr><h1>Protected</h1>';
echo '<h2>Initially...</h2>';
$parent->printVar('protected');
$child->printVar('protected');

// Attempt to modify $protected and reprint:
echo '<h2>Attempting to modify $parent->protected...</h2>';
$parent->protected = 'modified';
$parent->printVar('protected');
$child->printVar('protected');

// Print the current value of $_private:
echo '<hr><h1>Private</h1>';
echo '<h2>Initially...</h2>';
$parent->printVar('_private');
$child->printVar('_private');

// Attempt to modify $_private and reprint:
echo '<h2>Attempting to modify $parent->_private...</h2>';
$parent->_private = 'modified';
$parent->printVar('_private');
$child->printVar('_private');

// Delete the objects:
unset($parent, $child);

?>
</body>
</html>

Open in new window

0
Comment
Question by:rgb192
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 15

Accepted Solution

by:
Insoftservice earned 2000 total points
ID: 39805744
I have done slight changes please check

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Visibility</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
<?php # Script 5.4 - visibility.php
//  This page defines and uses the Test and LittleTest classes. 

# ***** CLASSES ***** #

/* Class Test.
 *  The class contains three attributes:
 *  - public $public
 *  - protected $protected
 *  - private $_private
 *  The class defines one method: printVar().
 */
class Test {

    // Declare the attributes:
    public $public = 'public';
    protected $protected = 'protected';
    private $privatevar = 'private';  

    // Function for printing a variable's value:
    function printVar($var) {
        echo "<p>In Test, \$$var: '{$this->$var}'.</p>";
    }

	function toChangeProtected($var)
	{
		 $this->protected =$var;
	}
	function toChangePrivate($var)
	{
		 $this->privatevar =$var;
	}
} // End of Test class.

/* LittleTest class extends Test.
 * LittleTest overrides printVar().
 */
class LittleTest extends Test {
    function printVar($var) {
		
        echo "<p>In LittleTest, \$$var: '{$this->$var}'.</p>";
    }


} // End of LittleTest class.

# ***** END OF CLASSES ***** #

// Create the objects:
$parent = new Test();
$child = new LittleTest();


// Attempt to modify $protected and reprint:
echo '<h2>Attempting to modify $parent->protected...</h2>';
$parent->toChangeProtected('modified');
$parent->printVar('protected');
$child->printVar('protected');

// Print the current value of $_private:
echo '<hr><h1>Private</h1>';
echo '<h2>Initially...</h2>';
$parent->printVar('privatevar');
//$child->printVar('privatevar');

// Attempt to modify $_private and reprint:
echo '<h2>Attempting to modify $parent->_private...</h2>';
$parent->toChangePrivate('modifiedprivate');
$parent->printVar('privatevar');
//$child->printVar('privatevar');

// Delete the objects:
unset($parent, $child);

?>
</body>

Open in new window

0
 
LVL 14

Expert Comment

by:Phil Phillips
ID: 39805870
It's because a "protected" variable can only be accessed by its own class or any extending classes.

The other variable scopes are "public" (accessible by anything), or "private" (accessible by ONLY its own class)

In that code sample, any functions that are part of the class "Test" or "LittleTest" are able to access the protected variable.  That's why the 'printVar()' function is working.  But, outside of those classes (like on line 71), the variable isn't accessible.
0
 
LVL 8

Expert Comment

by:Surrano
ID: 39805909
You can access a protected field of the parent class (Test) from a member method of the child (LittleTest) class. But what you try to do is to access the protected field from the global scope.

You can access $parent->protected from Test::printVar and from LittleTest::printVar, that's why printVar works.

Similarly, you can access $parent->private from Test::printVar but *not* from LittleTest::printVar.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 15

Expert Comment

by:Insoftservice
ID: 39806105
Its a simple oops concept

Private: Only specified class can access them , even its child class or sub class can not access the data

Protected: Parent and sub class can only access the data.

Protected and private methods prevent outside calls to methods which may upset the internal state of an object.

To modify any protected variables method has to used . Did you tried my examples
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39806258
Public, Protected and Private are "visibilities" associated with the properties and methods of a class.  Visibility is a logical extension of the concept of Variable Scope.  In the old ways of PHP, variable scope was basically either global or local.  In object-oriented PHP, variables (variables == properties) are scoped to be public by default.  The same is true of methods (methods == functions).

My preference for visibility, absent any other extenuating circumstances, is to make properties protected and methods public.  To inject data into the object, you must make a formal call to a setter() method.  To retrieve data from an object you must call the getter() method.  If you do it this way, you can use mock objects for unit testing.


Further reading:
0
 

Author Closing Comment

by:rgb192
ID: 39809595
I know what public, protected, private are
and I think the best answer is the code sample

I added additional code

//public values
echo '<h1>Public values</h1>';
$parent->printVar('public');
$child->printVar('public');

now I learned it was variable variables

because of revised code sample I was able to learn the errored code sample

Thank you.
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

664 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question