Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 991995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:11:46+00:00 2026-05-16T06:11:46+00:00

I am trying to assign a variable to a class in PHP, however I

  • 0

I am trying to assign a variable to a class in PHP, however I am not getting any results?

Can anyone offer any assistance? The code is provided below. I am trying to echo the URL as shown below, by first assigning it to a class variable.

class PageClass {


    var $absolute_path = NULL;

    function get_absolute_path(){

    $url = $this->absolute_path;

    echo $url;

    }

}

$page = new PageClass();
$page->absolute_path = "http://localhost:8888/smile2/organic/";

$page->get_absolute_path(); //this should echo the URL as defined above - but does not
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T06:11:46+00:00Added an answer on May 16, 2026 at 6:11 am

    It also works for me.

    Take a look at a live example of your code here.

    However, there are a few things you should change about your class.

    First, Garvey does make a good point that you should not be using var. That’s the older PHP4, less OOP conscious version. Rather declare each variable public or private. In fact, you should declare each function public or private too.

    Generally, most classes have private variables, since you usually only want to change the variables in specific ways. To achieve this control you usually set several public methods to allow client functions to interact with your class only in restricted predetermined ways.

    If you have a getter, you’d probably want a setter, since these are usually used with private variables, like I described above.

    A final note is that functions named get usually return a value. If you want to display a value, it is customary to use a name like display_path or show_path:

    <?php
    
       class PageClass 
       { 
            private $absolute_path = NULL;
    
            public function set_absolute_path($path)
            {
                $this->absolute_path = $path;   
            }
    
            public function display_absolute_path()
            {
                echo $this->absolute_path;
            }
        }
    
    $page = new PageClass();
    $page->set_absolute_path("http://localhost:8888/smile2/organic/");
    
    $page->display_absolute_path();
      // The above outputs: http://localhost:8888/smile2/organic/
    
    // Your variable is now safe from meddling.
    // This:
    // echo $this->absolute_path;
    // Will not work. It will create an error like:
    // Fatal error: Cannot access private property PageClass::$absolute_path on ...
    
    ?>
    

    Live Example Here

    There’s a section on classes and objects in the online PHP reference.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 514k
  • Answers 514k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use PHP DOM to parse it. Do it like this:… May 16, 2026 at 6:13 pm
  • Editorial Team
    Editorial Team added an answer Replace the Windows Shell. By that I mean Explorer.exe, by… May 16, 2026 at 6:13 pm
  • Editorial Team
    Editorial Team added an answer Inside an interrupt handler, don't use anything that can block.… May 16, 2026 at 6:13 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Hi im trying to execute the following PHP code, however im receiving an error.
i'm trying to assign a parent's variable from the parent's child //Parent public class
I'm trying to assign some JSON data to a property of a JS.Class instance.
I'm trying to assign a variable from a html text input value and then
I'm trying to implement a stateful web service in PHP using the SOAP extension.
Problem: I'm trying to code a multiple choice quiz for my fellow students -and
I'm trying to write a class that when asked on, will call on a
Trying to understand why you can't use sql_variant for the value type when using
I am trying to use a if condition to assign a value to a
I'm trying to create a parser for a simple language with the lemon port

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.