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,191 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 108

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 108

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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 108

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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 …

895 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now