Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder funnymoney's Avatar
    Join Date
    Aug 2007
    Posts
    364
    Thanks
    17
    Thanked 24 Times in 24 Posts

    Help with class and variables

    Hy guys

    I'm php newbie, looking for some guidance with php oop. I'm trying to get some variables into class but getting this error

    Parse error: parse error, unexpected T_VARIABLE in D:\www\Apache2\htdocs\test.php on line 7
    This is the full code

    PHP Code:
    <?php
    $variable  
    "my variable";

    class 
    phpClass {
        public 
    $main $variable;
        
    public function 
    main() {

            echo 
    $this->main;
        
        }

    }

    $mainis = new phpClass;

    $mainis->main();

    ?>
    Thanks for your help

  • #2
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,058
    Thanks
    10
    Thanked 96 Times in 94 Posts
    $variable is not within the scope of the class, e.g. it does not know it exists, you would normally set a default value

    public $main='';

    and populate $main via a setter or in the constructor

    public function main($var){
    $this->main=$var;
    ...etc
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #3
    Regular Coder funnymoney's Avatar
    Join Date
    Aug 2007
    Posts
    364
    Thanks
    17
    Thanked 24 Times in 24 Posts
    So that is not how it's done.

    Thanks for explaining the situation @oop newbie


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •