Jump to content

Recommended Posts

This is my code. I have three strings in MyFirstClass. I am trying to call them in MySecondClass

 

<?php

class MyFirstClass{

    private $string1="Hello",$string2="He",$string3="Hi";

 

    function getString1(){

        return $this->string1;

    }

    function getString2(){

        return $this->string2;

    }

    function getString3(){

        return $this->string3;

    }

}

 

class MySecondClass{

    private $var1,$var2,$var3;

 

    __Construct(MyFirstClass $firstClass){

        getFirstClassVars($firstClass);

        printFirstClassVars();

    }

 

    function getFirstClassVars($firstClass){

        $this->var1 = $firstClass->getString1();

        $this->var2 = $firstClass->getString2();

        $this->var3 = $firstClass->getString3();

    }

 

    function printFirstClassVars(){

        print $this->var1."<br/>";

        print $this->var2."<br/>";

        print $this->var3."<br/>";

    }

}

 

$firstClass = new MyFirstClass();

$secondClass = new MySecondClass($firstClass);

 

?>

 

I am getting this error "syntax error, unexpected T_STRING, expecting T_FUNCTION" at this line __Construct(MyFirstClass $firstClass){

 

Link to comment
https://forums.phpfreaks.com/topic/179238-solved-expecting-t_string-error/
Share on other sites

public function __construct() to be exact

Depending on the version of PHP.

 

No. I assume you're talking about PHP 4's lack of visibility keywords, but in PHP 4 the constructor was not called __construct().

 

public function __construct() to be exact

 

Actually, public is optional for methods because things by default are public.

 

public function __construct() to be exact

Depending on the version of PHP.

 

No. I assume you're talking about PHP 4's lack of visibility keywords, but in PHP 4 the constructor was not called __construct().

 

public function __construct() to be exact

 

Actually, public is optional for methods because things by default are public.

Yes, I was referring to that. But I forgot that constructors in PHP 4 were the same name as the class. Doh.

You need to change:

 

    __Construct(MyFirstClass $firstClass){
        getFirstClassVars($firstClass);
        printFirstClassVars();
    }

 

to:

 

    public function __construct(MyFirstClass $firstClass){
        $this->getFirstClassVars($firstClass);
        $this->printFirstClassVars();
    }

 

If you don't know why, just ask.

Thanks Salate and all, it worked.

 

Now I need to separate this classes as two php, MyFirstClass.php and MySecondClass.php. Save them in the same folder and try.

 

If there is any other slight change I need to make. Please help.

 

Thanks Guys...it worked. I will try include() too...

 

My first php class is test1.php

 

<?php

 

class MyFirstClass{

    private $string1="Hello",$string2="Testing",$string3="Hi";

 

    function getString1(){

        return $this->string1;

    }

    function getString2(){

        return $this->string2;

    }

    function getString3(){

        return $this->string3;

    }

}

 

?>

 

My second php class is test2.php

 

<?php

 

require('test1.php');

class MySecondClass{

    private $var1,$var2,$var3;

 

    public function __construct(MyFirstClass $firstClass){

$this->getFirstClassVars($firstClass);

        $this->printFirstClassVars();

    }

 

    function getFirstClassVars($firstClass){

        $this->var1 = $firstClass->getString1();

        $this->var2 = $firstClass->getString2();

        $this->var3 = $firstClass->getString3();

    }

 

    function printFirstClassVars(){

        print $this->var1."<br/>";

        print $this->var2."<br/>";

        print $this->var3."<br/>";

    }

}

 

$firstClass = new MyFirstClass();

$secondClass = new MySecondClass($firstClass);

 

?>

 

And this is what I needed. It worked.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.