Re: Instantiating objects in php

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hey dude

you can not pass parameters to class as you have done like
class ThisClass($someVar)

insted you can create a constructor and then pass while you create the
object of the class as given below
=================
this_class.php

class ThisClass()
{

    public function __construct($comeVar){
         private $sb = new StringBuilder();
    }

    public function SomeFunction()
    {
         $sb.append('Text');
         $sb.append('Text');
         return $sb->toString();
    }
}
-------------------------
this_class_caller.php
//initiate teh class object of the TestClass

$_this_cladd = new ThisClass($someVar);
print ($_this_cladd->SomeFunction);


==================

Let em know if u need anything else
Regards

-- 
Priyantha Gunawardena
______________________________________
{Web Designer / Developer}
Tel: +94 038-228-4323
Mobi: +94 077-393-8516
Blog: http://www.pgdesigning.com
IM : pgdesigning

On Sat, Aug 30, 2008 at 6:15 AM, Wilby Jackson <jacksw02@xxxxxxxxx> wrote:

>   Hi everyone. I'm a .Net developer and brand new to php. I've read
> through most of the php manual and in doing so realized there are a lot
> of similarities between c# and php. Anyway I have a specific question
> about my subject. I ran across an issue while trying to instantiate an
> object in a class. This is what I did.
>
> <?php
>
> include 'StringBuilder.php';
>
> class ThisClass($someVar)
> {
> private $sb = new StringBuilder();
>
> public function SomeFunction()
> {
> $sb.append('Text');
> $sb.append('Text');
> echo $sb->toString();
> }
> }
>
> ?>
>
> }
>
> I had to fix this by placing the object instantiation within a function
> like so:
>
> <?php
>
> include 'StringBuilder.php';
>
> class ThisClass($someVar)
> {
> private $sb;
>
> public function SomeFunction()
> {
> $sb = new StringBuilder();
>
> $sb.append('Text');
> $sb.append('Text');
> echo $sb->toString();
> }
> }
>
> ?>
>
> }
>
> I suppose the best way to handle this is to instantiate the object in
> the constructor but I was wondering if anyone had any idea why this is
> necessary in the first place?
>
> -Wilby Jackson
>
>  
>



-- 
Priyantha Gunawardena
______________________________________
{Web Designer / Developer}
Tel: +94 038-228-4323
Mobi: +94 077-393-8516
Blog: http://www.pgdesigning.com
IM : pgdesigning


[Non-text portions of this message have been removed]


[Index of Archives]     [PHP Home]     [PHP Users]     [PHP Soap]     [Kernel Newbies]     [Yosemite]     [Yosemite Campsites]

  Powered by Linux