PHP İşlev Yardım

2 Cevap php

Burada fonksiyon im bize çalışıyor, onun bir dosyada tutulan function.php çağırdı

   public function updateProfile($name = null, $email = null, $url = null, $location = null, $description = null)
        {
            // validate parameters
            if($name === null && $email === null && $url === null && $location === null && $description === null) throw new TwitterException('Specify at least one parameter.');
            if($name !== null && strlen($name) > 40) throw new TwitterException('Maximum 40 characters allowed for name.');
            if($email !== null && strlen($email) > 40) throw new TwitterException('Maximum 40 characters allowed for email.');
            if($url !== null && strlen($url) > 100) throw new TwitterException('Maximum 100 characters allowed for url.');
            if($location !== null && strlen($location) > 30) throw new TwitterException('Maximum 30 characters allowed for location.');
            if($description !== null && strlen($description) > 160) throw new TwitterException('Maximum 160 characters allowed for description.');

            // build parameters
            if($name !== null) $aParameters['name'] = (string) $name;
            if($email !== null) $aParameters['email'] = (string) $email;
            if($url !== null) $aParameters['url'] = (string) $url;
            if($location !== null) $aParameters['location'] = (string) $location;
            if($description !== null) $aParameters['description'] = (string) $description;

            // make the call
            $response = $this->doCall('account/update_profile.xml', $aParameters, true);

            // convert into xml-object
            $xml = @simplexml_load_string($response);

            // validate
            if($xml == false) throw new TwitterException('invalid body');

            // return
            return (array) $this->userXMLToArray($xml, true);
        }

Index.php var:

<php?

require_once("function.php");
$Twitter = new Twitter;

?>

Ben profil konumunu güncellemeniz gerekir?

2 Cevap

Dosyayı gerektirir sonra, yapmanız gereken tüm işlevini çağırır:

<?php
require_once("function.php");
updateProfile("username", "email", "url", "location", "description");
?>

Ancak, işlevi, bir sınıfın parçası gibi görünüyor - ama örnek kod sınıfı görünmüyor. Bir sınıfın parçası ise, ben 'Twitter' deniyor kabul edeceğiz. Bu durumda senin kullanarak kod yakındır. Bu çizgisinde olacaktır:

<?php
require_once("function.php");
$Twitter = new Twitter;
$Twitter->updateProfile("username", "email", "url", "location", "description");
?>

$this->doCall() - Tabii sınıfı, yayınlanan işlevi başvurmak gibi görünüyor kimlik çeşit, yapmak gerekir. Yine sınıfının toplu eksik, bu yüzden kendisi bu işlevi arıyor ne olmaz.

Sadece işlevini çağırmak için nasıl soruyorsunuz? Ben Twitter API ile aşina değilim, ama ben böyle bir şey olacağını beklemiyorduk:

$Twitter->updateProfile(null, null, null, 'new-location');

Her argüman isteğe bağlıdır ve null varsayılan değeri vardır, bu nedenle yalnızca belirli bir argüman geçmek istiyorsanız, o sadece bir önce tüm bağımsız değişkenler için null kullanın.