Solved

Fatal error: Undefined class constant 'price' in C:\wamp\www\oop-hg\ts1.php on line 10

Posted on 2013-11-27
6
1,202 Views
Last Modified: 2013-12-01
class Books{
  //member variables
  var $price;
  var $title;
  //member functions
  function setPrice($par){
    $this->price=$par;
  }
  function getPrice(){
    echo $this->price.'<br>';
  }
  function setTitle($par){
    $this->title=$par;
  }
  function getTitle(){
    echo $this->title.'<br>';
  }
}

Open in new window


    echo $this->price.'<br>';

I want to do
    echo parent::$price.'<br>';
      echo self::$price.'<br>';

but I get error
Fatal error: Undefined class constant 'price' in C:\wamp\www\oop-hg\ts1.php on line 10
0
Comment
Question by:rgb192
  • 3
  • 3
6 Comments
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39680831
If you're working with a tutorial that suggests this, get rid of it and find one that is at least up to the level of PHP5!

var $price;

The var designator is about 10 years obsolete.  In the PHP5 object model we use public, protected or private to designate class properties.

Are you sure you have posted the SSCCE here?  I'd like to have something I can install and run to recreate the error.
0
 

Author Comment

by:rgb192
ID: 39681443
http://www.tutorialspoint.com/php/php_object_oriented.htm


I am adding code to tutorial because I want to learn by typing

echo $this->price.'<br>';

I want to do
    echo parent::$price.'<br>';
      echo self::$price.'<br>';
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39681649
I'm still not sure I understand what you want to do, but if it's just for study I recently got this book and I think it's very good.
http://www.amazon.com/PHP-Advanced-Object-Oriented-Programming-QuickPro/dp/0321832183/

If you want to use the :: scope resolution operator you must declare the $price property static.  That means you cannot instantiate an object from the class.  If you try it like this, it will fail.  If you want an object instance of the class, you must use $this->price to access the property.

See: http://www.laprbass.com/RAY_temp_rgb192.php

Outputs:
THIS PRICE: 3
Fatal error: Access to undeclared static property: Second::$price in /home/websitet/public_html/RAY_temp_rgb192.php on line 24

<?php // RAY_temp_rgb192.php
error_reporting(E_ALL);

// http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28305101.html#a39681443

Class First
{
    protected $price;
    public function __construct($x)
    {
        $this->price = $x;
    }
}
Class Second Extends First
{
    protected $price;
    public function __construct($x)
    {
        $this->price = $x;
    }
    public function show_and_tell()
    {
        echo "THIS PRICE: $this->price";
        $abc = self::$price;
        echo "SELF PRICE: $abc";
        $abc = parent::$price;
        echo "PARENT PRICE: $abc";
    }
}

$obj = new Second(3);
$obj->show_and_tell();

Open in new window

0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:rgb192
ID: 39685460
Your code sample generates an error when I try static

echo $this->price.'<br>';

I want to do
    echo parent::$price.'<br>';
      echo self::$price.'<br>';

Open in new window


how to modify code so I can try all 3 of these options this,parent, self (for my own learning)
0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39685489
Yes, of course it generates an error.  That's why I posted the output showing "fatal error."

If you want to do three things, you probably want to write three separate pieces of code.  Class properties are either static or not.  They aren't both.

If you want an object instance of the class, you use $this->

If you want a static property you use the scope resolution operator :: (Paamayim Nekudotayim)
0
 

Author Closing Comment

by:rgb192
ID: 39688817
okay.  I will have a related question

thanks
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Checkout Page Input Field not aligned 1 26
PHP Syntax Error 4 32
Uploading a CSV Data Import via PHP & MySql 3 29
How to delete values from an array using php 4 22
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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 count occurrences of each item in an array.

856 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